zoukankan      html  css  js  c++  java
  • SQL SqlParameter

    SqlParameter 类

    表示 SqlCommand 的参数,也可以是它到 DataSet 列的映射。 

    下面代码使用参数对数据表进行插入操作。

        using (SqlConnection conn = new SqlConnection(connString))
                {
                    try
                    {
                        if (conn.State == ConnectionState.Closed)
                        {
                            conn.Open();
                            //创建SqlCommand命令
                            using (SqlCommand cmd = conn.CreateCommand())
                            {
                                string sqlText = "INSERT INTO [工作人员] VALUES(@编号,@姓名,@性别,@联系电话,@电子信箱,@QQ)";
                                cmd.CommandText = sqlText;
                                //创建SqlCommand对象所需要的参数
                                SqlParameter[] parms = {  new SqlParameter("@编号",SqlDbType.NVarChar,10),
                                                          new SqlParameter("@姓名",SqlDbType.NVarChar,20),
                                                          new SqlParameter("@性别",SqlDbType.NVarChar,2),
                                                          new SqlParameter("@联系电话",SqlDbType.NVarChar,20),
                                                          new SqlParameter("@电子信箱",SqlDbType.NVarChar,50),
                                                          new SqlParameter("@QQ",SqlDbType.Int)
                                                       };
                                parms[0].Value = txtID.Text.Trim();
                                parms[1].Value = txtName.Text.Trim();
                                parms[2].Value = sex;
                                parms[3].Value = txtPhone.Text.Trim();
                                parms[4].Value = txtPhone.Text.Trim();
                                parms[5].Value = Convert.ToInt32(txtQQ.Text.Trim());
                                cmd.Parameters.AddRange(parms);//添加参数
                                if (cmd.ExecuteNonQuery() > 0)
                                {
                                    MessageBox.Show("成功添加新员工信息");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("程序出错了,错误信息" + ex.Message);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                    }
                }

    注:以上代码来自《C#编程网络大讲堂》,仅供学习交流!!!

  • 相关阅读:
    HERO 3
    office的一些应用,
    网页之间的参数传弟
    一个好的数码网站
    C++遍历中删除std::hash_map元素问题
    【转】Asio与shared_ptr的一些注意事项
    delphi的字节对齐
    paypal的即时付款通知参数列表(PDT)
    vs2010下libevent的使用
    mysql 数据库 left join,right join, inner join 知识
  • 原文地址:https://www.cnblogs.com/YuanSong/p/2611230.html
Copyright © 2011-2022 走看看