前端界面操作数据库

前端界面操作数据库

一、思维导图

 

 

二、知识点介绍

前端操作数据要用到的不仅仅是浮现在用户前面的一堆控件,控件的背后还需要代码的支持才能真正的做到,运用前端操作数据库。

首先,我们要连接数据库:

                  InitializeComponent(); SqlConnection sqlConnection = new SqlConnection();

                    sqlConnection.ConnectionString =

                          "Server=(Local);Database=EMR;Integrated Security=sspi";

                  sqlConnection.Open();

                  MessageBox.Show

                          ("连接状态:" + sqlConnection.State.ToString()

                          + "\n工作站标识:" + sqlConnection.WorkstationId

                          + "\n服务器地址:" + sqlConnection.DataSource

                          + "\n服务器版本:" + sqlConnection.ServerVersion

                          + "\n数据库名称:" + sqlConnection.Database

                          + "\n\n(单击【确定】后将关闭SQL连接)");

                  sqlConnection.Close();

                  MessageBox.Show

                          ("连接状态:" + sqlConnection.State.ToString());

         接着,我们进行注册:

 

            SqlConnection sqlConnection = new SqlConnection();

            sqlConnection.ConnectionString =

                ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

            SqlCommand sqlCommand = new SqlCommand();

 

            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText ="INSERT 学生 (学号,姓名,班级,电话)

 

            VALUES(@学号,@姓名,@班级,@电话);";

            sqlCommand.Parameters.AddWithValue("@学号", cob_学号 .Text.Trim());

            sqlCommand.Parameters.AddWithValue("@姓名", cob_姓名 .Text.Trim());

sqlCommand.Parameters.AddWithValue("@班级", cob_班级 .Text.Trim());

sqlCommand.Parameters.AddWithValue("@电话", cob_电话 .Text.Trim());

 

 

            sqlConnection.Open();

            int rowAffected = sqlCommand.ExecuteNonQuery();

            sqlConnection.Close();

            MessageBox.Show("保存成功");

然后,我们来试验一下是否可以登录:

 

 

         SqlConnection sqlConnection = new SqlConnection();

         sqlConnection.ConnectionString =

                  ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;

         SqlCommand sqlCommand = new SqlCommand();                                                 

         sqlCommand.Connection = sqlConnection;                                                               if (button1_Click_1.Checked == true)

         {

                  sqlCommand.CommandText =

                          "SELECT * FROM EMR WHERE XH=@XH AND Name=@Name;

                  sqlParameter.ParameterName= "@XH";

                  sqlParameter.Value = this.textBox1.Text.Trim();

                  sqlParameter.SqlDbType = SqlDbType.VarChar;

                  sqlParameter.Size = 20;

                  sqlCommand.Parameters.Add(sqlParameter);

                  sqlCommand.Parameters.AddWithValue("@Name", this.textbxpsw.Text.Trim());

                  sqlCommand.Parameters["@Name"].SqlDbType = SqlDbType.VarChar;

     MessageBox.Show("登陆成功","登陆成功");

         }

posted on 2018-10-25 00:23  fenglianchen  阅读(6328)  评论(0编辑  收藏  举报

导航