C# 带数据网格的C&SQL server:从数据网格视图添加到单独的数据库
因此,我知道您可以从SQL server将信息填充到数据网格中。但是,是否有一个相反的过程,但以不同的表这样做 例如,拥有一个主列表并使用其他表 例如: 主表:显示在datagrid1上 其他表格 我想将匹配的列从一行从Master TableDataGridView1拉到其他TableSQL数据库 所以本质上,您可以在DataGridView1中单击一行,然后单击一个按钮将行添加到其他表中。这将把insert命令添加到SQL数据库中,在本例中,它将排除OtherTableName列,而只将TestSubh和TestCriteria插入OtherTable中C# 带数据网格的C&SQL server:从数据网格视图添加到单独的数据库,c#,sql-server,datagridview,insert,C#,Sql Server,Datagridview,Insert,因此,我知道您可以从SQL server将信息填充到数据网格中。但是,是否有一个相反的过程,但以不同的表这样做 例如,拥有一个主列表并使用其他表 例如: 主表:显示在datagrid1上 其他表格 我想将匹配的列从一行从Master TableDataGridView1拉到其他TableSQL数据库 所以本质上,您可以在DataGridView1中单击一行,然后单击一个按钮将行添加到其他表中。这将把insert命令添加到SQL数据库中,在本例中,它将排除OtherTableName列,而只将Te
这有可能吗?我已经尝试搜索了一些关于这方面的文档,但似乎找不到任何内容。我确信有一种更简单的方法可以实现这一点。但是,您可以只使用选定行单元格值中的字符串。用户必须选择整行。您可以在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);
}
}
这也跳过了第1列。但是,如果要从第1列中提取信息,只需添加:
string iZero = dgMasterGridView.SelectedRows[0].Cells[0].Value + string.Empty;
&&
别忘了将其添加到SQLQuery字符串中
您还可以将信息存储在Var中,以便以后在范围内或范围外使用
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);