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字符串的三种写法的更多相关文章

  1. DB2 insert into 三种写法

    db2的insert into 支持三种格式,即:一次插入一行,一次插入多行和从SELECT语句中插入. 以表为例: create table “user" ( "name&quo ...

  2. insert into 语句的三种写法

    insert into 语句的三种写法 方式1. INSERT INTO t1(field1,field2) VALUES (v001,v002);            // 明确只插入一条Valu ...

  3. setInterval()的三种写法

    前言: setInterval("fun()",time)有两个参数:fun()为要执行的函数:time为多久执行一次函数,单位是毫秒: 我们做一个简单的例子,就是每隔5s弹出一个 ...

  4. 链接属性rel=’external’、rel=’nofollow’、rel=’external nofollow’三种写法的区别

    链接属性rel='external'.rel='nofollow'.rel='external nofollow'三种写法的区别   大家应该都知道rel='nofllow'的作用,它是告诉搜索引擎, ...

  5. jquery 在页面中三种写法

    jQuery 分 2 个系列版本 1.x 与 2.x,主要的区别在于 2.x 不再兼容 IE6.7.8浏览器,这样做的目的是为了兼容移动端开发.由于减少了一些代码,使得该版本比 jQuery 1.x ...

  6. 总结 React 组件的三种写法 及最佳实践 [涨经验]

    React 专注于 view 层,组件化则是 React 的基础,也是其核心理念之一,一个完整的应用将由一个个独立的组件拼装而成. 截至目前 React 已经更新到 v15.4.2,由于 ES6 的普 ...

  7. 彻底了解构建 JSON 字符串的三种方式

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7701856.html 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax ...

  8. python列表和字符串的三种逆序遍历方式

    python列表和字符串的三种逆序遍历方式 列表的逆序遍历 a = [1,3,6,8,9] print("通过下标逆序遍历1:") for i in a[::-1]: print( ...

  9. HTML颜色的三种写法

    颜色的三种写法: 1.16进制代码     #000000 2.英文字母         red 3.rgba                rgba(0-255,0,0,0-1) 例如: <b ...

随机推荐

  1. composer错误收集

    1. Problem 1 - The requested package ** is satisfiable by ** but these conflict with your requiremen ...

  2. NGINX将PHP带参数的URL地址重定向二级或多级域名访问

    今天项目中有一个手机站点需要用*.m.domain.com的三级域名访问. 如手机站点的访问网址为m.domain.com,手机下面的会员实际访问地址为index.php?username=$user ...

  3. php 选择排序法

    private function demo(array $arr = array()){ $len = count($arr); if ($len == 1) return $arr; else fo ...

  4. hdu4970 Killing Monsters (差分数列)

    2014多校9 1011 http://acm.hdu.edu.cn/showproblem.php?pid=4970 Killing Monsters Time Limit: 2000/1000 M ...

  5. poj2391 Ombrophobic Bovines 题解

    http://poj.org/problem?id=2391 floyd+网络流+二分 题意:有一个有向图,里面每个点有ai头牛,快下雨了牛要躲进雨棚里,每个点有bi个雨棚,每个雨棚只能躲1头牛.牛可 ...

  6. Oracle CASE WHEN 用法介绍

    1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END --Case搜索 ...

  7. webapp中fixed问题解决方案

    主要问题: 1,头部输入框固定后,只要再滑动内容的话,输入框会随着滑动内容而滑动. 2,在低端机:2.3以下的安卓机,你会发现怎么解决都不行的,系统浏览器是不会支持的,头部底部固定的话会滑动内容而滑动 ...

  8. firstchild.data与childNodes[0].nodeValue意思(转)

    x.firstchild.data:获取元素第一个子节点的数据: x.childNodes[0]::获取元素第一个子节点; x.childNodes[0].nodeValue.:也是获取元素第一个子节 ...

  9. springmvc之log4j

    1.工程结构 2.所需jar包 3.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  10. javascript高级程序设计---Event对象三

    进度事件 进度事件用来描述一个事件进展的过程,比如XMLHttpRequest对象发出的HTTP请求的过程.<img>.<audio>.<video>.<st ...