C#将数据从ODBC数据库传输到本地SQL数据库

时间:2016-11-08 01:42:44

标签: c# sql sql-server database odbc

我已通过ODBC连接连接到数据库。数据在服务器上,我有适当的权限和用户名/密码。

我正在尝试将一些数据导入到本地SQL数据库(.mdf)中。我怀疑我的SQL语句是错误的。

这个想法是当从listBox中选择一个项目时,数据将被下载到SQL数据库。 这完全阻止了我项目的任何进展。请帮忙!!!

    public partial class frmNorth : Form
    {
            // variables for the connections 
            private OdbcConnection epnConnection = new OdbcConnection();
            private SqlConnection tempDbConnection = new SqlConnection();
    public frmNorth()
    {
        InitializeComponent();
        // This is for the ePN DB
        epnConnection.ConnectionString = @"Dsn=ePN; uid=username; pwd=myPa$$Word";
        // This is for the local DB
        tempDbConnection.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TempDB.mdf;Integrated Security=True";
    }
    private void lbxFSR_SelectedIndexChanged(object sender, EventArgs e)
    {
        try //For ePN
        {
            //This is where I need the help <--------------------
            epnConnection.Open();
            tempDbConnection.Open();
            OdbcCommand epnCommamd = new OdbcCommand();
            epnCommamd.Connection = epnConnection;
            string epnQuery =   "INSERT INTO " + tempDbConnection + ".tblTemp (FNCL_SPLIT_REC_ID, PROJ_ID, SALES_SRC_PRC) " +
                                "SELECT PROJ_FNCL_SPLIT.FNCL_SPLIT_REC_ID,PROJ_FNCL_SPLIT.PROJ_ID,PROJ_FNCL_SPLIT.SALES_SRC_PRC " + 
                                "FROM " + epnConnection + ".PROJ_FNCL_SPLIT " + 
                                "WHERE PROJ_ID=" + lbxFSR.Text + "";
            epnCommamd.CommandText = epnQuery;
            epnCommamd.CommandTimeout = 0;
            epnCommamd.ExecuteNonQuery();

            epnConnection.Close();
            tempDbConnection.Close();
        }
        catch (Exception ex)
        {
            epnConnection.Close();
            tempDbConnection.Close();
            MessageBox.Show("Error " + ex);
        }
    }
    }

这是我得到的错误。错误发生在 epnCommamd.ExecuteNonQuery();

Picture of Error Message

2 个答案:

答案 0 :(得分:1)

我无法评论因为我没有足够的分数所以我必须把它放在答案中,但你的两个连接实际上都打开了吗?我也会避免在你的连接字符串中显示密码。

答案 1 :(得分:0)

问题在于,您通常可以使用来自另一个数据库中的表的SELECT以您尝试的方式INSERT到一个表中。如果源表和目标表位于同一数据库服务器上(例如,在Sql Server上),则可以使用INSERT INTO db1.SourceTable ... SELECT ... FROM db2.DestinationTable

但是,由于在Sql连接上有ODBC连接和目标的源表,因此无法正常工作。

您需要分两步完成。将ODBC表下载到C#DataTable,然后将C#DataTable上载到Sql Server表中。我无法对您的数据库进行测试,但我已经在Microsoft Access数据库和Sql Server数据库之间的传输上测试了此代码的一个版本

private void lbxFSR_SelectedIndexChanged(object sender, EventArgs e)
{
    try //For ePN
    {
        //This is where I need the help <--------------------

        // Break the operation into two parts
        // The ODBC & SQL databases can't talk directly to each other.

        // 1. Download ODBC table into your C# DataTable

        DataTable dt;
        epnConnection.Open();
        string epnQuery =   "SELECT FNCL_SPLIT_REC_ID, PROJ_ID, SALES_SRC_PRC " + 
                            "FROM PROJ_FNCL_SPLIT " + 
                            "WHERE PROJ_ID='" + lbxFSR.Text + "'";
        OdbcCommand epnCommamd = new OdbcCommand(epnQuery, epnConnection);
        epnCommamd.CommandTimeout = 0;
        OdbcDataReader dr = epnCommamd.ExecuteReader();
        dt.Load(dr);
        epnConnection.Close();

        // 2. Upload your C# DataTable to the SQL table

        // This select query tells the SqlDataAdapter what table you want to work with, on SQL database
        // The WHERE 0 = 1 clause is to stop it returning any rows, 
        // however you still get the column names & datatypes which you need to perform the update later
        string selectQuery = "SELECT FNCL_SPLIT_REC_ID, PROJ_ID, SALES_SRC_PRC " +
                            " FROM PROJ_FNCL_SPLIT WHERE 0 = 1";
        tempDbConnection.Open();
        var da = new SqlDataAdapter(selectQuery, tempDbConnection);
        var commandBuilder = new SqlCommandBuilder(da);
        // The DataAdapter's `Update` method applies the contents of the DataTable `dt` to the table specified in the `selectQuery`.
        // It does this via the SqlCommandBuilder, which knows how to apply updates to a Sql Database.
        da.Update(dt);                                      // Channel the C# DataTable through the DataAdapter
        tempDbConnection.Close();
    }
    catch (Exception ex)
    {
        epnConnection.Close();
        tempDbConnection.Close();
        MessageBox.Show("Error " + ex);
    }
}