[英]best way to transfer data from one database to other in ms sql express 05 using c#
[英]C# Transfer Data from ODBC Database to Local SQL Database
我已經通過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();處。
我無法發表評論,因為我沒有足夠的分數,所以我必須在答案中回答這個問題,但實際上您的兩個連接都打開嗎? 我還要避免在此處的連接字符串中顯示密碼。
問題在於,通常無法以嘗試的方式使用另一數據庫上表的SELECT將INSERT插入一個表。 如果源表和目標表在同一數據庫服務器上(例如,在Sql Server上),則可以在INSERT INTO db1.SourceTable ... SELECT ... FROM db2.DestinationTable
。
但是,由於在ODBC連接上有源表,而在Sql連接上有目標表,所以這是行不通的。
您需要分兩個步驟進行操作。 將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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.