[英]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.