码迷,mamicode.com
首页 > 数据库 > 详细

C#与数据库访问技术总结(九)之实例

时间:2014-11-02 10:44:22      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   ar   使用   for   sp   

  

实例

更新记录

    在本例子中,建立一个供用户输入学生学号和姓名的文本框和几个对应不同操作类型的更新信息按钮,当用户输入信息以后单击相应的按钮则执行相应的操作。在此实例中还将接触到服务器信息验证的相关知识。

    (1)新建名为UpdateTest的Windows Application应用程序,在默认的Forml.cs中添加2个Label控件,2个TextBox控件,3个Button控件,按表4.7所示设置这7个控件的属性。

表4.7控件属性

控件类型                    ID属性                          Text属性

标签                         lblUserID                 学号:

标签                         lblUserName               姓名:

文本框                       txtUserlD

文本框                       txtUserName

按钮                         btnExecute1                拼接字符串

按钮                         btnExecute2                使用参数

按钮                         btnExecute3                使用存储过程

 

 

 

 

 

 

 

 

 

  (2)调整控件的位置(按照个人喜好了)

    (3)双击“拼接字符串”按钮,注册按钮btnExecute的按钮单击事件btnExecute1_Click,

    然后再切换到Form1.cs页面的“设计”视图,依次双击“使用参数”和“使用存储过程”按钮来注册对应的按钮单击事件btnExecute2_Click和btnExecute3_Click。

    (4)在Form1.cs文件中首先引入命名空间System.Data.SqlClient,然后添加一个名 CheckInfo的方法,返回值为bool类型,代码如下:

 

bool CheckInfo()
{
  //判断学号是否输入
  if (this.txtUserID.Text.Trim() == "")
  {
    Alert("学号不完整");
    return false;
  }
  else if (this.txtUserName.Text.Trim() == "") //判断姓名是否输入
  {
    Alert("姓名不完整");
    return false;
  }
  //信息检查通过
  return true;
}

 

//其中,Alert是自定义的另外一个方法,用来弹出一个对话框,定义如下:
        void Alert(string message)
        {
            MessageBox.Show(null, message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        //在btnExecute1_Click中编写如下代码:
        private void btnExecute1_Click(object sender, EventArgs e)
        {
            //信息检查
            if(this.CheckInfo())
            {
                //取值
                string userId=this.txtUserID.Text.Trim();
                string userName=this.txtUserName.Text.Trim();
                //新建连接对象
                SqlConnection conn=new SqlConnection();
                conn.ConnectionString="Data Source=(local);Initial Catalog=Student;Integrated Security=SSPI";
                //拼接命令字符串
                string updateQuery="update StudentInfo set sName=‘"+userName+""+"where ID=‘"+userId+"";
                //新建命令对象
                SqlCommand cmd=new SqlCommand(updateQuery,conn);
                conn.Open();
                 //保存执行结果
                int RecordsAffected=cmd.ExecuteNonQuery();
                conn.Close();
                //提示结果
                Alert("更新数据数为"+RecordsAffected);
            }
        }
        //在btnExecute2_Click中编写如下代码:
        private void btnExecute2_Click(object sender, EventArgs e)
        {
            //信息检查
            if(this.CheckInfo())
            {
                //取值
                string userId=this.txtUserID.Text.Trim();
                string userName=this.txtUserName.Text.Trim();
                //新建连接对象
                SqlConnection conn=new SqlConnection();
                conn.ConnectionString="Data Source=(local);Initial Catalog=Student;Integrated Security=SSPI";
                //拼接命令字符串
                string updateQuery="update StudentInfo set sName=@userName where ID=@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();
                /*
                try
                {
                    conn.Open();
                    //保存执行结果
                    int RecordsAffected = cmd.ExecuteNonQuery();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, "修改记录失败");
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }*/
                //提示结果
                Alert("更新数据数为"+RecordsAffected);
            }
        }
        //在btnExecute3_Click中编写如下代码:
        private void btnExecute3_Click(object sender, EventArgs e)
        {
            //信息检查
            if (this.CheckInfo())
            {
                //取值
                string userId = this.txtUserID.Text.Trim();
                string userName = this.txtUserName.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);
            }
        }

  (9)在学号和姓名中分别输入信息以后,单击任意按钮即可测试更新结果。
  例如:

  分别输入学号"2007102001"和姓名“某某”后单击任意按钮.


代码讲解
  在引入了System.Data.SqlClient命名空间以后,使用了SQL Server .NET数据提供程序对数据进行更新。
  更新数据前使用了CheckInfo方法对数据进行检查,查看用户是否在姓名和学号两个文本框中输入了有效的信息,如果两者的输入信息都有效,则该方法返回true,否则返回false,

  方法实现如下:  

          //判断学号是否输入
            if (this.txtUserID.Text.Trim() == "")
            {
                Alert("学号不完整");
                return false;
            }
            else if (this.txtUserName.Text.Trim() == "")  //判断姓名是否输入
            {
                Alert("姓名不完整");
                return false;
            }
            //信息检查通过
            return true;

  当用户输入的信息不正确时,Checklnfo将调用Alert方法显示提示信息对话框,

  Alert方法实际上是固定MessageBox.Show方法的一些参数,利用其来弹出对话框,Alert方法实现非常简单,仅仅需要下面一句代码:  

MessageBox.Show(null,message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

  其中,message是Alert方法接受的参数。

  在3个按钮单击事件中,程序代码分别实现对应的数据更新操作,这些操作前面都进行了详细的讲解,这里不再赘述。

 

C#与数据库访问技术总结(九)之实例

标签:style   blog   io   color   os   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/zi-xing/p/4034332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!