• 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#编程网络大讲堂》,仅供学习交流!!!

  • 相关阅读:
    cmd输出的日志里有中文乱码的解决办法
    自定义控件ToggleButton滑动开关
    移除指定位置的jsonarray
    设置Listview不滚动
    Volley框架学习
    LoaderManager的使用
    Activity获取Fragment的值
    Fragment和Fragment进行数据传递
    Fragmet的学习
    android ListView上拉加载更多
  • 原文地址:https://www.cnblogs.com/YuanSong/p/2611230.html
Copyright © 2020-2023  润新知