• C#访问SQL——三种更新记录的方法 Joyce


    用户通过C#Windows窗体应用程序访问并更新数据库可有三种方式,下面是三种方式的主要代码:

    1.拼接字符串

            private void button1_Click_1(object sender, EventArgs e)
            {
                //信息检查
                if (this.CheckInfo())
                {
                    //取值
                    string userId = this.textBox1.Text.Trim();
                    string userName = this.textBox2.Text.Trim();
                    //新建连接对象
                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = "Data Source=(local);Initial Catalog=student;Integrated Security=SSPI";
                    //拼接命令字符串
                    string updateQuery = "update student set sname='" + userName + "'"+"where sno='" + userId+"'";
                    //新建命令对象
                    SqlCommand cmd = new SqlCommand(updateQuery, conn);
                    conn.Open();
                    //保存执行结果
                    int RecordsAffected = cmd.ExecuteNonQuery();
                    conn.Close();
                    //提示结果
                    Alert("更新数据数为" + RecordsAffected.ToString());
                }
            }

    2.使用参数

            private void button2_Click(object sender, EventArgs e)
            {
                //取值
                string userId = this.textBox1.Text.Trim();
                string userName = this.textBox2.Text.Trim();
                //新建连接对象
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "Data Source=(local);Initial Catalog=Student;Integrated Security=SSPI";
                //拼接命令字符串
                string updateQuery = "update student set sname=@userName where sno=@userId";
                //新建命令对象
                SqlCommand cmd = new SqlCommand(updateQuery, conn);
                //添加参数
                cmd.Parameters.Add(new SqlParameter("@userName", userName));
                cmd.Parameters.Add(new SqlParameter("@userId", userId));
                conn.Open();
                //保存执行结果
                int RecordsAffected = cmd.ExecuteNonQuery();
                conn.Close();
                Alert("更新数据数为" + RecordsAffected);
            }

    3.使用存储过程

            private void button3_Click(object sender, EventArgs e)
            {
                //信息检查
                if (this.CheckInfo())
                {
                    //取值
                    string userId = this.textBox1.Text.Trim();
                    string userName = this.textBox2.Text.Trim();
                    //新建连接对象
                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = "Data Source=(local);Initial Catalog=Student;Integrated Security=SSPI";
                    //新建命令对象
                    SqlCommand cmd = new SqlCommand("UpdateStudentInfo", conn);
                    //指定命令类型为存储过程
                    cmd.CommandType = CommandType.StoredProcedure;
                    //添加参数
                    cmd.Parameters.Add(new SqlParameter("@userName", userName));
                    cmd.Parameters.Add(new SqlParameter("@userId", userId));
                    conn.Open();
                    //保存执行结果
                    int RecordsAffected = cmd.ExecuteNonQuery();
                    conn.Close();
                    //提示结果
                    Alert("更新数据数为" + RecordsAffected);
                }

    数据库中新建存储过程:

    CREATE PROCEDURE UpdateStudentInfo
        (
        @userName  nvarchar(20), 
        @userId nvarchar(20)
        )
    AS
      Update student
      set sname=@userName where sno=@userId
    GO
  • 相关阅读:
    Ubuntu vsftp复制文件到远端时错误,Permission denied
    linux 常用命令
    vbox安装ubuntu之后挂载共享文件夹无权限访问的问题以及改了主机名,导致命令行不能解析主机名的问题
    java 中的valueOf方法和强转
    读写文件,用代码在讲html文件转为jsp文件
    hibernate查询之后用el表达式取值时遇到的问题
    考研:操作系统:进程同步—信号量实现同步互斥(PV操作)
    考研:操作系统:处理机调度——几种经典的调度算法
    KMP算法
    mysql出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' 错误
  • 原文地址:https://www.cnblogs.com/joycelee/p/2747590.html
Copyright © 2020-2023  润新知