c#之Insert字符串的三种写法
1.Format
| Format(String, Object) | 将指定字符串中的一个或多个格式项替换为指定对象的字符串表示形式。 |
ex1:简单示例怎么应用
private void btnTest_Click(object sender, EventArgs e)
{
string str = string.Format("您输入的信息为:{0}",txtTest.Text);
MessageBox.Show(str);
}

ex2:数据库命令字符串的两种写法
第一种:
private void btnInsert_Click(object sender, EventArgs e)
{
string strcon = @"Data Source=LON;Initial Catalog=Practice;Integrated Security=True";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string strcmd = "insert into Info_Stu (Name,Age,Sex) values ('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"')";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = strcmd;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
}

第二种:
private void btnInsert_Click(object sender, EventArgs e)
{
string strcon = @"Data Source=LON;Initial Catalog=Practice;Integrated Security=True";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string strcmd = string.Format("insert into Info_Stu (Name,Age,Sex) values ('{0}','{1}','{2}')", textBox1.Text, textBox2.Text, textBox3.Text);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = strcmd;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
}
效果图同上,对比两条指令:
"insert into Info_Stu (Name,Age,Sex) values ('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"')";
string strcmd = string.Format("insert into Info_Stu (Name,Age,Sex) values ('{0}','{1}','{2}')", textBox1.Text, textBox2.Text, textBox3.Text);
第三种:
string connstr = @"Data Source=LON;Initial Catalog=PRACTICE;User ID=sa;Password=***";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string cmdstr = "insert into info_stu (name,age,sex) values (@name,@age,@sex)";
SqlCommand cmd=new SqlCommand(cmdstr,conn);
SqlParameter[] paras = new SqlParameter[]{new SqlParameter("@name",txtName.Text),
new SqlParameter("@age",txtAge.Text),
new SqlParameter("@sex",txtSex.Text)};
cmd.Parameters.AddRange(paras);
cmd.ExecuteNonQuery();

string connstr = @"Data Source=LON;Initial Catalog=PRACTICE;User ID=sa;Password=****";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string cmdstr = "insert into info_stu (name,age,sex) values (@name,@age,@sex)";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = cmdstr;
cmd.Parameters.Add("@name", txtName.Text);
cmd.Parameters.Add("@age", txtAge.Text);
cmd.Parameters.Add("@sex", txtSex.Text);
cmd.ExecuteNonQuery();
c#之Insert字符串的三种写法的更多相关文章
- DB2 insert into 三种写法
db2的insert into 支持三种格式,即:一次插入一行,一次插入多行和从SELECT语句中插入. 以表为例: create table “user" ( "name&quo ...
- insert into 语句的三种写法
insert into 语句的三种写法 方式1. INSERT INTO t1(field1,field2) VALUES (v001,v002); // 明确只插入一条Valu ...
- setInterval()的三种写法
前言: setInterval("fun()",time)有两个参数:fun()为要执行的函数:time为多久执行一次函数,单位是毫秒: 我们做一个简单的例子,就是每隔5s弹出一个 ...
- 链接属性rel=’external’、rel=’nofollow’、rel=’external nofollow’三种写法的区别
链接属性rel='external'.rel='nofollow'.rel='external nofollow'三种写法的区别 大家应该都知道rel='nofllow'的作用,它是告诉搜索引擎, ...
- jquery 在页面中三种写法
jQuery 分 2 个系列版本 1.x 与 2.x,主要的区别在于 2.x 不再兼容 IE6.7.8浏览器,这样做的目的是为了兼容移动端开发.由于减少了一些代码,使得该版本比 jQuery 1.x ...
- 总结 React 组件的三种写法 及最佳实践 [涨经验]
React 专注于 view 层,组件化则是 React 的基础,也是其核心理念之一,一个完整的应用将由一个个独立的组件拼装而成. 截至目前 React 已经更新到 v15.4.2,由于 ES6 的普 ...
- 彻底了解构建 JSON 字符串的三种方式
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7701856.html 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax ...
- python列表和字符串的三种逆序遍历方式
python列表和字符串的三种逆序遍历方式 列表的逆序遍历 a = [1,3,6,8,9] print("通过下标逆序遍历1:") for i in a[::-1]: print( ...
- HTML颜色的三种写法
颜色的三种写法: 1.16进制代码 #000000 2.英文字母 red 3.rgba rgba(0-255,0,0,0-1) 例如: <b ...
随机推荐
- Android Studio-设置快速转换局部变量为成员变量
"File"-"Settings"-"KeyMap"-"Main Menu"-"Refactor"- ...
- Netbeans 设置模板
/** * @Description * @author ${user} * @date ${date} ${time} * @copyright ${copyright} */ 工具-->模板 ...
- linux系统命令:yum和apt-get
转:http://my.oschina.net/liuyuanyuangogo/blog/546850 1.linux系统分类 一般来说著名的linux系统基本上分两大类: RedHat系列:Redh ...
- EF上下文管理
- corntab
http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html
- jquery动态改变my97日期格式
$('#qsrq').unbind('focus'); $('#zzrq').unbind('focus'); $('#qsrq').bind('focus', function () { Wdate ...
- redhat 下 rpm 指令
1.如何安装rpm软件包rmp软件包的安装可以使用程序rpm来完成.执行下面的命令 rpm -i your-package.rpm 其中your-package.rpm是你要安装的rpm包的文件名,一 ...
- 微信电脑版真的要来了 微信Windows版客户端1.0 Alpha推出
微信电脑版的搜索量一直很大,但只有网页版,之前也写了微信网页版APP - 网页微信客户端电脑版体验,在键盘上打字的感觉就是快.现在微信Windows版客户端1.0 Alpha推出了,来一睹芳容吧(20 ...
- tcl实现http请求
package require "http" proc errLog args { puts $args } proc SendHttp args { global token s ...
- Call to undefined function mysqli_connect()
PHP5.0后新支持一个mysqli.dll的扩展功能,能让用户更加简洁的调用mysql数据库. 需要在Php.ini配置中将下面代码前的;去掉.extension=php_mysqli.dll