简体   繁体   English

具有数据网格的C#和SQL Server:从数据网格视图添加到单独的数据库

[英]C# & SQL server with data grid: Adding to A seperate database from data grid view

So, I know that you can populate information to a data grid from a SQL server. 因此,我知道您可以将信息从SQL Server填充到数据网格中。 But is there a reverse process but to a different table to do so? 但是,是否有一个相反的过程却要在另一个表中进行呢?

For instance having a master list and using other tables. 例如,具有主列表并使用其他表。

Example: Master Table: (Displayed on datagrid1 ) 示例:主表:(显示在datagrid1上

| OtherTableName|  TestSubj  | TestCriteria  |
----------------------------------------------
|    TableName  | ValueSubj  | ValueCriteria | 
----------------------------------------------
|    TableName  | ValueSubj  | ValueCriteria |
---------------------------------------------- 

Other Table 其他表

| TestSubj  | TestCriteria  | 
-----------------------------
| ValueSubj | ValueCriteria |
-----------------------------
| ValueSubj | ValueCriteria | 
-----------------------------

I want to pull the matching columns from a single row from Master Table (DataGridView1) to Other Table (SQL Database). 我想将匹配的列从主表 (DataGridView1)的单行拉到其他表 (SQL数据库)。

So essentially, you would click a row in DataGridView1 then click a button "Add Row to Other Table". 因此,基本上,您将在DataGridView1中单击一行,然后单击“将行添加到其他表”按钮。 Which would add your insert command to the SQL database, in this case it would Exclude the "OtherTableName" column and only insert the "TestSubj" and "TestCriteria" into "OtherTable" Table.. 这会将您的插入命令添加到SQL数据库,在这种情况下,它将排除“ OtherTableName”列,而仅将“ TestSubj”和“ TestCriteria”插入“ OtherTable”表中。

Is this at all possible? 这是可能吗? I've tried searching for some documentation on this, but I can't seem to find anything. 我尝试搜索有关此问题的文档,但似乎找不到任何东西。

I'm sure there's a much simpler method in doing this.However, You can just use the strings from the selected rows cell Value. 我敢肯定有一个简单得多的方法。但是,您可以只使用所选行单元格Value中的字符串。 The user must select entire row You can change these settings in Visual Studio under datagridview properties. 用户必须选择整行。您可以在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);
        }
    }

This also skips the 1st column. 这也会跳过第一列。 However, If you wanted to pull the information from the 1st column, just add: 但是,如果要从第一列中提取信息,只需添加:

string iZero = dgMasterGridView.SelectedRows[0].Cells[0].Value + string.Empty;

&& &&

cmd.Parameters.AddWithValue("@Item0", iZero);

Don't forget to add it to your SQLQuery string also. 不要忘记也将其添加到您的SQLQuery字符串中。

You can also Store the information in a Var for later use inside or outside of your scope. 您还可以将信息存储在Var中,以供以后在示波器内部或外部使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM