English 中文(简体)
在服务器上添加可数据的内容
原标题:Insert DataTable contents into 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());
        }
}

我对如何做到这一点,已经发现任何真正好的样本。

SqlBulkCopy is a option but again I will hold my hands up and say I don t know how to do this

问题回答

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

因此,一旦你在数据表、电网格表等中拥有数据(无论真实情况如何),你就必须在表格的每个行子上做每个缝.。 在座各位中,你将拥有SqlConnection、SqlCommand、SqlParailes和处决。

这一联系应当提供正确信息! 希望!

页: 1

如果你想要使用SqlBulkCopy标语,那么,你的可数据标语与目标数据库表一样。 如果你处理很多问题,这是一个好的做法。 如果不是的话,也许会更好地利用一个正常的INSERT查询。

附上一些代码,显示使用SqlBulkCopy的方法。 请注意,当地数据表(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);
}




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签