2

将记录从 excel 文件上传到数据库中的表的最有效方法是什么。我不允许使用 DTS/SSIS。所以我想知道是否有比从文件中顺序读取记录和触发命令更好的选择。

谢谢。

4

4 回答 4

1

Do you have permissions for bulk inserting?

于 2009-05-19T15:33:08.960 回答
1

You could use the bcp utility. Save the Excel file as text and bcp it in. You don't usually need bulk insert privileges to do that.

于 2009-05-19T15:35:26.837 回答
1

我建议您通过 ADODB 连接使用 ODBC/DSN 连接到 excel 文件。我发现这是非常有效的。

您首先通过以下方式创建一个 ODBC 数据源名称:控制面板>管理工具>数据源 (ODBC)。选择“系统”选项卡,然后单击“添加”。从出现的驱动程序列表中选择“Microsoft Excel 驱动程序”。为您的 DSN 命名,例如“MYDB”,然后浏览 xlS 文件并双击选择。

这可以通过编程方式完成,只是我们像 5 年前一样创建了一个 dll 来完成它,而我仍在尝试找到它的源代码。我一得到它就会发布它的代码。

然后从您的程序中,您可以按如下方式连接到您的 DSN:

  'declare the connection
   Global MyConn As New ADODB.Connection

  'open the connection
   MyConn.Open "DSN=MYDB;pwd=;"

然后,您可以通过 ADODB 记录集以正常方式操作连接。

我希望这有帮助

于 2009-05-19T11:27:59.593 回答
1

这个页面有相反的代码——从 SQL Server 中提取数据并将其插入 Excel。您需要做的就是交换连接字符串。

像这样:

    private System.Data.OleDb.OleDbDataAdapter da ;
    private System.Data.DataSet ds;

    string sqlSelect="SELECT ProductId, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock, GETDATE() as TimeExtracted  from Products order by UnitPrice";

    string sqlInsert="INSERT INTO Foo (ProductId, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock, TimeExtracted) VALUES (@ProductId, @ProductName, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @TimeExtracted)"; 

    string ExtractedTableName= "ExtractedData";


    private void ReadFromSource()
    {
        System.Console.WriteLine("Reading from Source...");

        string ConnStringSource= 
            "Provider=Microsoft.Jet.OLEDB.4.0;" + 
            "Data Source=" + ExcelFilename + ";" + 
                                             "Extended Properties=\"Excel 8.0;HDR=yes;\"";  // FIRSTROWHASNAMES=1;READONLY=false\"

        using (var conn= new System.Data.OleDb.OleDbConnection(ConnStringSource))
        {
            da= new System.Data.OleDb.OleDbDataAdapter();
            da.SelectCommand=  new System.Data.OleDb.OleDbCommand(sqlSelect);
            da.SelectCommand.Connection= conn;

            // this tells the DA to mark all rows as newly inserted.
            // upon calling da.Update() (later), all those rows will
            // be inserted into the DB.
            da.AcceptChangesDuringFill= false;

            ds= new System.Data.DataSet();
            da.Fill(ds, ExtractedTableName);
        }
    }


    private void InsertIntoDestination()
    {
        System.Console.WriteLine("Inserting data into Destination...");

        string ConnStringDest= "Provider=sqloledb;Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;";

        using (var conn= new System.Data.OleDb.OleDbConnection(ConnStringDest))
        {

            System.Data.OleDb.OleDbCommand cmd= new System.Data.OleDb.OleDbCommand(sqlInsert);

            cmd.Parameters.Add("@ProductId", System.Data.OleDb.OleDbType.Integer, 4, "ProductId");
            cmd.Parameters.Add("@ProductName", System.Data.OleDb.OleDbType.VarWChar, 40, "ProductName");
            cmd.Parameters.Add("@QuantityPerUnit", System.Data.OleDb.OleDbType.VarWChar, 20, "QuantityPerUnit");
            cmd.Parameters.Add("@UnitPrice", System.Data.OleDb.OleDbType.Currency, 8, "UnitPrice");
            cmd.Parameters.Add("@UnitsInStock", System.Data.OleDb.OleDbType.SmallInt, 2, "UnitsInStock");
            cmd.Parameters.Add("@TimeExtracted", System.Data.OleDb.OleDbType.Date, 8, "TimeExtracted");

            da.InsertCommand=  cmd;
            da.InsertCommand.Connection= conn;

            da.Update(ds, ExtractedTableName);

            // in the event you want to update a datasource via a different DataAdapter --
            // for example you want to fill from a System.Data.SqlClient.DataAdapter and
            // then Update via a System.Data.Oledb.OledbDataAdapter -- then you could define
            // two distinct DataAdapters.  Fill the DataSet with the first DA, then Update
            // with the second DA. 
        }
    }
于 2009-05-19T16:07:05.227 回答