[英]C# & SQL server with data grid: Adding to A seperate database from data grid view
因此,我知道您可以將信息從SQL Server填充到數據網格中。 但是,是否有一個相反的過程卻要在另一個表中進行呢?
例如,具有主列表並使用其他表。
示例:主表:(顯示在datagrid1上 )
| OtherTableName| TestSubj | TestCriteria |
----------------------------------------------
| TableName | ValueSubj | ValueCriteria |
----------------------------------------------
| TableName | ValueSubj | ValueCriteria |
----------------------------------------------
其他表
| TestSubj | TestCriteria |
-----------------------------
| ValueSubj | ValueCriteria |
-----------------------------
| ValueSubj | ValueCriteria |
-----------------------------
我想將匹配的列從主表 (DataGridView1)的單行拉到其他表 (SQL數據庫)。
因此,基本上,您將在DataGridView1中單擊一行,然后單擊“將行添加到其他表”按鈕。 這會將您的插入命令添加到SQL數據庫,在這種情況下,它將排除“ OtherTableName”列,而僅將“ TestSubj”和“ TestCriteria”插入“ OtherTable”表中。
這是可能嗎? 我嘗試搜索有關此問題的文檔,但似乎找不到任何東西。
我敢肯定有一個簡單得多的方法。但是,您可以只使用所選行單元格Value中的字符串。 用戶必須選擇整行。您可以在Visual Studio的datagridview屬性下更改這些設置。
private void button1_Click_1(object sender, EventArgs e)
{
string iOne = dgMasterGridView.SelectedRows[0].Cells[1].Value + string.Empty;
string iTwo = dgMasterGridView.SelectedRows[0].Cells[2].Value + string.Empty;
string iThree = dgMasterGridView.SelectedRows[0].Cells[3].Value + string.Empty;
string iFour = dgMasterGridView.SelectedRows[0].Cells[4].Value + string.Empty;
string iFive = dgMasterGridView.SelectedRows[0].Cells[5].Value + string.Empty;
string iSix = dgMasterGridView.SelectedRows[0].Cells[6].Value + string.Empty;
string iSeven = dgMasterGridView.SelectedRows[0].Cells[7].Value + string.Empty;
string iEight = dgMasterGridView.SelectedRows[0].Cells[8].Value + string.Empty;
string iNine = dgMasterGridView.SelectedRows[0].Cells[9].Value + string.Empty;
string iTen = dgMasterGridView.SelectedRows[0].Cells[10].Value + string.Empty;
string iEleven = dgMasterGridView.SelectedRows[0].Cells[11].Value + string.Empty;
string iTwelve = dgMasterGridView.SelectedRows[0].Cells[12].Value + string.Empty;
try
{
// Connection to DB
SqlConnection con = new SqlConnection();
con.ConnectionString = (@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Database;Integrated Security=True");
//Insert Query
string insertquery = "INSERT INTO dbo.[myTable] ([Item1], [Item2], [Item3], [Item4], [Item5], [Item6], [Item7], [Item8], [Item9], [Item10], [Item11], [Item12]) VALUES(@Item1,@Item2,@Item3,@Item4,@Item5,@Item6,@Item7,@Item8,@Item9,@Item10,@Item11,@Item12)";
SqlCommand cmd = new SqlCommand(insertquery, con);
//open connection
con.Open();
//Parameters
cmd.Parameters.AddWithValue("@Item1", iOne);
cmd.Parameters.AddWithValue("@Item2", iTwo);
cmd.Parameters.AddWithValue("@Item3", iThree);
cmd.Parameters.AddWithValue("@Item4", iFour);
cmd.Parameters.AddWithValue("@Item5", iFive);
cmd.Parameters.AddWithValue("@Item6", iSix);
cmd.Parameters.AddWithValue("@Item7", iSeven);
cmd.Parameters.AddWithValue("@Item8", iEight);
cmd.Parameters.AddWithValue("@Item9", iNine);
cmd.Parameters.AddWithValue("@Item10", iTen);
cmd.Parameters.AddWithValue("@Item11", iEleven);
cmd.Parameters.AddWithValue("@Item12", iTwelve);
//execute
cmd.ExecuteNonQuery();
//close connection
con.Close();
MessageBox.Show("Information has been submitted");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
這也會跳過第一列。 但是,如果要從第一列中提取信息,只需添加:
string iZero = dgMasterGridView.SelectedRows[0].Cells[0].Value + string.Empty;
&&
cmd.Parameters.AddWithValue("@Item0", iZero);
不要忘記也將其添加到您的SQLQuery字符串中。
您還可以將信息存儲在Var中,以供以后在示波器內部或外部使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.