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. Apache解析漏洞详解

    很多次听到人说apache的“解析漏洞”了,正好今天又有人问,那就简单科普一下这个“解析漏洞”是何物. 先来看测试过程和结果的对比吧. 结果一 首先,我安装了apache 2.x版本,同时以modul ...

  2. 【转】Flume日志收集

    from:http://www.cnblogs.com/oubo/archive/2012/05/25/2517751.html Flume日志收集   一.Flume介绍 Flume是一个分布式.可 ...

  3. thinkphp学习笔记13-15集

    13集: ThinkPHP3.1.3使用视频教程--后台登录验证与自动运行方法_标清.flv 14集: ThinkPHP3.1.3使用视频教程--自定义SESSION处理DB驱动与添加Redis处理驱 ...

  4. recording just for inquiry in the future

    auditd审计 相关命令有: auditd, auditctl, ausearch, aureport 相关文件: /etc/audit/auditd.conf, /etc/audit/audit. ...

  5. vim中添加molokai.vim 配色安装

    无意中发现知乎中讨论的话题: 你认为最好看的 Vim 配色方案(color scheme)是哪款? 网友回答 排在第一位的是:molokai 啊,最经典的配色 既然molokai这么经典,当然要用了. ...

  6. PostgreSQL表空间、数据库、模式、表、用户/角色之间的关系

    看PostgreSQL9的官方文档,我越看越迷糊,这表空间,数据库,模式,表,用户,角色之间的关系怎么在PostgreSQL里这么混乱呢?经过中午的一个小实验,我逐渐理清了个中来龙去脉.下面我来还原我 ...

  7. Collection类相关总结

    集合类的框架如下: Collection(接口)    List(接口):允许重复.         ArrayList         Vector         LinkedList    Se ...

  8. [译]Mongoose指南 - 中间件

    中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生. 中间件都是document级别的不是model级别的. 下面讲讲两种中间件pre ...

  9. 新年新技术:MongoDB 3.0

    前一篇介绍了HTTP/2,这一篇简单介绍下3月3号发布的MongoDB 3.0. What’s new in MongoDB 3.0? 新的存储引擎WiredTiger MongoDB 3.0的存储引 ...

  10. ie下获取上传文件全路径

    ie下获取上传文件全路径,3.5之后的火狐是没法获取上传文件全路径的 /*获取上传文件路径*/ function getFilePath(obj) { var form = $(this).paren ...