直播中
this.sqlConnection1.ConnectionString = "data source=WHOAMI; initial catalog=tyjdb; integrated security=SSPI; persist security info=False; workstation id=WHOAMI; packet size=4096"; 其中WHOAMI為筆者的服務(wù)器名。 |
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; //存取數(shù)據(jù)庫(kù)的主要類 protected System.Data.SqlClient.SqlCommand sqlSelectCommand1; //SQL語(yǔ)句處理的類 protected System.Data.SqlClient.SqlConnection sqlConnection1; //連接數(shù)據(jù)庫(kù)的類 在InitializeComponent()中有如下聲明: this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; this.sqlSelectCommand1.CommandText = "SELECT name, email, age, address FROM address"; this.sqlSelectCommand1.Connection = this.sqlConnection1; |
sqlConnection1.Open(); //打開(kāi)數(shù)據(jù)庫(kù)連接 DataSet objDataset; //新建一個(gè)放數(shù)據(jù)的DataSet objDataset=new DataSet(); sqlDataAdapter1.Fill(objDataset, "address"); //將數(shù)據(jù)填入DataSet DataGrid1.DataSource=objDataset.Tables["address"].DefaultView; //關(guān)聯(lián)DataSet和DataGrid DataGrid1.DataBind(); //綁定數(shù)據(jù) sqlConnection1.Close(); //關(guān)閉數(shù)據(jù)庫(kù)連接 |
sqlInsertCommand1.Parameters["@name"].Value=TextBox1.Text; //將TextBox賦值給對(duì)應(yīng)參數(shù) sqlInsertCommand1.Parameters["@email"].Value=TextBox2.Text; sqlInsertCommand1.Parameters["@age"].Value=TextBox3.Text; sqlInsertCommand1.Parameters["@address"].Value=TextBox4.Text; sqlInsertCommand1.Connection.Open(); //打開(kāi)連接 sqlInsertCommand1.ExecuteNonQuery(); //執(zhí)行Insert語(yǔ)句 sqlInsertCommand1.Connection.Close(); //關(guān)閉連接 sqlConnection1.Open(); DataSet objDataset; //下面程序段更新DataGrid objDataset=new DataSet(); sqlDataAdapter1.Fill(objDataset, "address"); DataGrid1.DataSource=objDataset.Tables["address"].DefaultView; DataGrid1.DataBind(); |
SqlCommand sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand(); //聲明Sql Command類對(duì)象 this.sqlDataAdapter1.DeleteCommand = sqlDeleteCommand1; sqlDeleteCommand1.CommandText="DELETE FROM ADDRESS WHERE NAME='"+TextBox5.Text+"'"; //SQL語(yǔ)句 sqlDeleteCommand1.Connection = this.sqlConnection1; //聲明使用的數(shù)據(jù)連接 sqlDeleteCommand1.Connection.Open(); sqlDeleteCommand1.ExecuteNonQuery(); //執(zhí)行該SQL語(yǔ)句 sqlDeleteCommand1.Connection.Close(); |