VS2008 C# 如何在前端介面操作資料表
阿新 • • 發佈:2018-11-28
操作資料表主要分為更新和查詢
更新
更新包括增添、修改、刪除三個功能,首先按正常操作敲好三個功能的程式碼
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ToString();
SqlCommand insqlCommand = new SqlCommand();
insqlCommand.Connection = sqlConnection;
insqlCommand.CommandText =
"INSERT tb_藥品資訊表"
+ " (ypbh ,ypmc,yplx,dj ,sj,dw,pfj,lsj,jyj ,zdj,kl ,yxq ,cd ,sccj ) "
+ " VALUES(@No,@ypmc ,@yplx ,@dj ,@sj ,@dw,@pfj,@lsj ,@jyj,@zdj,@kl,@yxq,@cd,@sccj )";
insqlCommand.Parameters.Add("@Name", SqlDbType.VarChar, 0, "ypmc");
insqlCommand.Parameters.Add("@yplx", SqlDbType.Bit, 0, "yplx");
insqlCommand.Parameters.Add("@dj", SqlDbType.Float, 0, "dj");
insqlCommand.Parameters.Add("@sj", SqlDbType.Float, 0, "sj");
insqlCommand.Parameters.Add("@dw", SqlDbType.VarChar, 0, "dw");
insqlCommand.Parameters.Add("@pfj", SqlDbType.Float, 0, "pfj");
insqlCommand.Parameters.Add("@lsj", SqlDbType.Float, 0, "lsj");
insqlCommand.Parameters.Add("@jyj", SqlDbType.Float, 0, "jyj");
insqlCommand.Parameters.Add("@zdj", SqlDbType.Float, 0, "zdj");
insqlCommand.Parameters.Add("@kl", SqlDbType.Int, 0, "kl");
insqlCommand.Parameters.Add("@yxq", SqlDbType.Date, 0, "yxq");
insqlCommand.Parameters.Add("@cd", SqlDbType.VarChar, 0, "cd");
insqlCommand.Parameters.Add("@sccj", SqlDbType.VarChar, 0, "sccj");
insqlCommand.Parameters.Add("@No", SqlDbType.Char, 10, "ypbh");
SqlCommand upsqlCommand = new SqlCommand();
upsqlCommand.Connection = sqlConnection;
upsqlCommand.CommandText =
"UPDATE tb_藥品資訊表"
+ " SET [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected] "
+ " WHERE [email protected];";
upsqlCommand.Parameters.Add("@Name", SqlDbType.VarChar, 0, "ypmc");
upsqlCommand.Parameters.Add("@yplx", SqlDbType.Bit, 0, "yplx");
upsqlCommand.Parameters.Add("@dj", SqlDbType.Float, 0, "dj");
upsqlCommand.Parameters.Add("@sj", SqlDbType.Float, 0, "sj");
upsqlCommand.Parameters.Add("@dw", SqlDbType.VarChar, 0, "dw");
upsqlCommand.Parameters.Add("@pfj", SqlDbType.Float, 0, "pfj");
upsqlCommand.Parameters.Add("@lsj", SqlDbType.Float, 0, "lsj");
upsqlCommand.Parameters.Add("@jyj", SqlDbType.Float, 0, "jyj");
upsqlCommand.Parameters.Add("@zdj", SqlDbType.Float, 0, "zdj");
upsqlCommand.Parameters.Add("@kl", SqlDbType.Int, 0, "kl");
upsqlCommand.Parameters.Add("@yxq", SqlDbType.Date, 0, "yxq");
upsqlCommand.Parameters.Add("@cd", SqlDbType.VarChar, 0, "cd");
upsqlCommand.Parameters.Add("@sccj", SqlDbType.VarChar, 0, "sccj");
upsqlCommand.Parameters.Add("@NewNo", SqlDbType.Char, 10, "ypbh");
upsqlCommand.Parameters.Add("@OldNo", SqlDbType.Char, 10, "ypbh");
upsqlCommand.Parameters["@OldNo"].SourceVersion = DataRowVersion.Original;
SqlCommand desqlCommand = new SqlCommand();
desqlCommand.Connection = sqlConnection;
desqlCommand.CommandText =
"DELETE tb_藥品資訊表"
+ " WHERE [email protected];";
desqlCommand.Parameters.Add("@No", SqlDbType.Char, 10, "ypbh");
需要注意的是,三個功能的命令名稱互不相同,用以區分
接下來,建立一個新的SqlDataAdapter類,SqlDataAdapter類一邊是功能,另一邊是資料表,概念如下圖所示
SqlDataAdapter類提供了三個屬性,分別對應更新功能的更改、增加、刪除,只要將剛才敲好的三個功能的命令名稱分別對接,再把SqlDataAdapter類的另一邊接上DataGridView控制元件即可,對接的程式碼為
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.UpdateCommand = upsqlCommand;
sqlDataAdapter.InsertCommand = insqlCommand;
sqlDataAdapter.DeleteCommand = desqlCommand;
DataTable studentTable = (DataTable)this.dgv_drug.DataSource;
至此,DataGridView控制元件的更新功能完成
查詢
查詢功能相對簡單,首先,我在載入資料時,定義了一個靜態的DataTable資料表來儲存表
private DataTable Table ;
......
this.Table = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(Table);
sqlConnection.Close();
這張表可以全域性使用,接著,在查詢按鈕的程式碼下,使用一個用於查詢的DataRow類,用於查詢DataTable表中的一行資料,程式碼如下
DataRow[] searchResultRows =
this.Table.Select("ypmc LIKE '%" + this.textBox_Seach.Text.Trim() + "%'");
DataTable searchTable = this.Table.Clone();
foreach (DataRow row in searchResultRows)
{
searchTable.ImportRow(row);
}
this.dgv_drug.DataSource = searchTable;
即可以實現查詢功能