前端界面操作數據庫


前端界面操作數據庫

一、思維導圖

 

 

二、知識點介紹

前端操作數據要用到的不僅僅是浮現在用戶前面的一堆控件,控件的背后還需要代碼的支持才能真正的做到,運用前端操作數據庫。

首先,我們要連接數據庫:

                  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("登陸成功","登陸成功");

         }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM