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 ...
随机推荐
- OC-封装
一. set方法和get方法 1. set方法和get方法的使用场合 @public的成员可以被随意赋值,应该使用set方法和get方法来管理成员的访问(类似机场的安检.水 ...
- bootstrap和bootstrap-select的outline设置
.btn:focus, .btn:active:focus, .btn.active:focus, .btn.focus, .btn:active.focus, .btn.active.focus { ...
- C#反射机制(转)
一:反射的定义 审查元数据并收集关于它的类型信息的能力.元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等. Sys ...
- CF453C Little Pony and Summer Sun Celebration (DFS)
http://codeforces.com/contest/456 CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...
- CF459C Pashmak and Buses (构造d位k进制数
C - Pashmak and Buses Codeforces Round #261 (Div. 2) C. Pashmak and Buses time limit per test 1 seco ...
- NLPIR_Init文本分词-总是初始化失败,false,Init ICTCLAS failed!
前段时间用这个分词用的好好的,突然间就总是初始化失败了: 网上搜了很多,但是不是我想要的答案,最终去了官网看了下:官网链接 发现哇,版本更新了啊,下载页面链接 麻利的下载好了最新的文档,一看压缩包名字 ...
- AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建
一.测试环境介绍 1. 宿主使用工作站(HYPR-V)基本配置如下: 处理器:Intel(R) Core(TM) i5-4470 CPU @ 3.20GHz 3.20GHz 内存(RAM):8.00G ...
- JSON/XML序列化与反序列化(非构造自定义类)
隔了很长时间再重看自己的代码,觉得好陌生..以后要养成多注释的好习惯..直接贴代码..对不起( ▼-▼ ) 保存保存:进行序列化后存入应用设置里 ApplicationDataContainer _a ...
- SQL pivot 基本用法 行列转换 数据透视
SQL通过pivot进行行列转换 数据透视 可直接在sql server 运行 传统操作 和 pivot create table XKCl (name nchar(10) not null, 学科 ...
- 15个关于Chrome的开发必备小技巧
一.快速查找文件 如果你使用过Sublime,那么你会知道’Go to anything’的强大.没错,Chrome现在也有了这一功能. 操作如下: 1.F12打开你的Chrome调试器: 2.按下C ...