将DataTable内容插入SQL Server

时间:2012-01-12 13:23:17

标签: sql-server datagrid insert

大家好我正在尝试从我的datagridveiw中获取数据并插入到我的SQL Server中,但我被卡在插入部分,这是我到目前为止所拥有的

private void button1_Click(object sender, EventArgs e)
{
        var fdlg = new OpenFileDialog();
        fdlg.Title = @"Site Assist Database Update";
        fdlg.InitialDirectory = @"c:\";
        fdlg.Filter = @"CSV Files (*.csv)|*.csv";
        fdlg.FilterIndex = 2;
        fdlg.RestoreDirectory = true;

        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            string filepath = fdlg.FileName;
            var engine = new FileHelperEngine<PartList>();
            engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;
            PartList[] res = engine.ReadFile(filepath);

            if (engine.ErrorManager.ErrorCount > 0)
                engine.ErrorManager.SaveErrors("Errors.txt");

            var table = new DataTable();
            table.Columns.Add("Part Number", typeof (string));
            table.Columns.Add("Part Name", typeof(string));
            table.Columns.Add("On Hand New", typeof(string));
            table.Columns.Add("On Hand Fixed", typeof(string));
            table.Columns.Add("SC5 - Storage Bin - EAME", typeof(string));
            table.Columns.Add("Is ASL Item", typeof(string));
            table.Columns.Add("Reorder Point", typeof(string));
            table.Columns.Add("Stock Maximum", typeof(string));
            table.Columns.Add("On Order", typeof(string));
            table.Columns.Add("Back Order", typeof(string));
            table.Columns.Add("Part Cost", typeof(string));
            table.Columns.Add("PC11 - Repairable - EAME", typeof(string));

            foreach (PartList row in res)
            {
                table.Rows.Add(row.PartNum, row.PartName, row.New, row.Fix, row.Location, row.ASL, row.ReorderPoint,
                               row.stockMax, row.OnOrder, row.BackOrder, CleanInput(row.Cost), row.Repairable);
            }

            dataGridView1.DataSource = table;

            MessageBox.Show(table.Rows.Count.ToString());
        }
}

我已经google了一下,无法找到关于如何实现这一目标的任何非常好的固体样本。

SqlBulkCopy是一个选项,但我会再举起手来说我不知道​​该怎么做

2 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/6185836/843327

因此,一旦在数据表,gridview等中存在数据(无论如何),您需要在该表的每一行上执行foreach循环。在foreach中,您将拥有SqlConnection,SqlCommand,SqlParameters和执行。

链接应该提供正确的信息!希望它有所帮助!

斯蒂芬

答案 1 :(得分:0)

如果要使用SqlBulkCopy对象,则必须使DataTable对象与目标数据库表具有相同的模式。如果你要处理很多行,这是一个很好的方法。如果没有,最好使用普通的SQL INSERT查询。

附上一些代码以显示使用SqlBulkCopy的方法。请注意,本地DataTable(MyDataTable)的架构是从数据库中填充的。这可确保您拥有相同的架构,并避免使用代码进行繁琐的设置。

DataTable MyDataTable = new DataTable();          

// this section will get the schema of your target table without retriving any data
using(SqlConnection MyConnection = new SqlConnection("a connection string")) {                
    string getSchemaSql = "SELECT * FROM MyDatabaseTable WHERE 1 = 0";            
    SqlDataAdapter A = new SqlDataAdapter(getSchemaSql, MyConnection);
    MyConnection.Open();
    A.FillSchema(MyDataTable, SchemaType.Source);
}

// add your information to MyDataTable
foreach (PartList row in res) {
    MyDataTable.Rows.Add(row.PartNum, row.PartName, row.New, row.Fix, row.Location, row.ASL, row.ReorderPoint, row.stockMax, row.OnOrder, row.BackOrder, CleanInput(row.Cost), row.Repairable); 
}

// assign MyDataTable as your DataGridView source
dataGridView1.DataSource = MyDataTable;

// export MyDataTable to the DBMS database table
using(SqlConnection MyConnection = new SqlConnection("a connection string")) {                
    SqlBulkCopy TheBulkCopier = new SqlBulkCopy(MyConnection);                
    TheBulkCopier.DestinationTableName = "MyDatabaseTable";
    MyConnection.Open();
    TheBulkCopier.WriteToServer(MyDataTable);
}