从本地系统Ms Access上传数据到Web Sql Server?

时间:2014-06-08 10:55:37

标签: sql sql-server vb.net winforms

我有一个Windows表单应用程序,用于维护公司员工的计费详细信息。应用程序在MS Access数据库中存储和操作数据。 我必须将数据发送到SQL Server Web数据库,以便员工可以在网站上查看他们的月度报告。 我的代码是:

Private Sub UploadData()
   Dim conn as New OleDb.OleDbConnection("Connection String Ms Access DB")
   Dim SCON = New SqlConnection(" Web SQL Sever Connection")
        Try
            SCON.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try
   Dim Qcmd As New SqlCommand("select query", SCON)
   Dim Cmd As New OleDbCommand("select id,acc,newacc,city,name,billduedateamt,oldbalance from Records where city = 'Delhi'", conn)
   Dim Mrdr As OleDbDataReader = Cmd.ExecuteReader
        Try
            While Mrdr.Read
                Qcmd.CommandText = "Insert into billing (acc,newacc,city,name,billduedateamt,oldbalance) Values (" + _
                        Mrdr.Item(0) + "," + Mrdr.Item(1) + ... Up to So On ")"
                Qcmd.ExecuteNonQuery()
            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If SCON.State <> ConnectionState.Closed Then
                SCON.Close()
            End If
            conn.Close()
        End Try

End Sub

它太慢了,有没有更好的代码可以这样做?

3 个答案:

答案 0 :(得分:2)

以下是一些显示批量复制的代码。

private static void PerformBulkCopyDifferentSchema()
{
    string connectionString = @"Server=
        localhost;Database=Northwind;Trusted_Connection=true";
    DataTable sourceData = new DataTable();
    // get the source data
    using (SqlConnection sourceConnection =
                    new SqlConnection(connectionString))
    {
        SqlCommand myCommand =
            new SqlCommand("SELECT TOP 5 * 
            FROM Products_Archive", sourceConnection);
        sourceConnection.Open();
        SqlDataReader reader = myCommand.ExecuteReader();
        // open the destination data
        using (SqlConnection destinationConnection =
                    new SqlConnection(connectionString))
        {
            // open the connection
            destinationConnection.Open();
            using (SqlBulkCopy bulkCopy =
                new SqlBulkCopy(destinationConnection.ConnectionString))
            {
                bulkCopy.ColumnMappings.Add("ProductID", "ProductID");
                bulkCopy.ColumnMappings.Add("ProductName", "Name");
                bulkCopy.ColumnMappings.Add("QuantityPerUnit", "Quantity");
                bulkCopy.DestinationTableName = "Products_TopSelling";
                bulkCopy.WriteToServer(reader);
            }
        }
        reader.Close();
    }
}

答案 1 :(得分:0)

由于您只是插入数据,请使用SQL Server的批量复制功能,该功能通过.NET中的SqlBulkCopy类显示。使用数据适配器使用Access中的数据填充DataTable,然后使用SqlBulkCopy尽快将批次插入SQL Server。

答案 2 :(得分:0)

尝试这样的事情:

Private Sub UploadData()
Dim conn as New OleDb.OleDbConnection("Connection String Ms Access DB")
Dim SCON = New SqlConnection(" Web SQL Sever Connection")
    Try
        SCON.Open()
    Catch ex As Exception
        MsgBox(ex.Message)
        Exit Sub
    End Try
Dim Qcmd As New SqlCommand("select query", SCON)
Dim Cmd As New OleDbCommand("select id,acc,newacc,city,name,billduedateamt,oldbalance from Records where city = 'Delhi'", conn)

Dim Mrdr As OleDbDataReader = Cmd.ExecuteReader
    Try
       Dim sqlInsert as String="Insert into billing (acc,newacc,city,name,billduedateamt,oldbalance) "
        While Mrdr.Read
            sqlInsert = sqlInsert +  " Values (" + _
                    Mrdr.Item(0) + "," + Mrdr.Item(1) + ... Up to So On ")"

        End While
         Qcmd.CommandText = sqlInsert
            Qcmd.ExecuteNonQuery()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        If SCON.State <> ConnectionState.Closed Then
            SCON.Close()
        End If
        conn.Close()
    End Try

End Sub