0
我正嘗試將Microsoft Access數據庫上載到Microsoft SQL Server Express數據庫中。將Microsoft Access數據庫上載到Microsoft SQL Server Express數據庫中
Access和SQL數據庫的結構除了主鍵的名稱是相同的。
錯誤代碼:
System.InvalidOperationException: No data exists for the row/column. at System.Data.OleDb.OleDbDataReader.DoValueCheck(Int32 ordinal) at System.Data.OleDb.OleDbDataReader.GetValue(Int32 ordinal) at System.Data.OleDb.OleDbDataReader.get_Item(Int32 index) at ACCESStoMDF._Default.Button1_Click(Object sender, EventArgs e) in C:\Users\path2ACCESStoMDF\ACCESStoMDF\Default.aspx.cs:line 44
的web.config
<configuration>
<connectionStrings>
<add name="ACCESSdb"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\path1\DBaccess.accdb;Persist Security Info=False;" />
<add name="MSSQLdb"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MSSQLdb.mdf;User Instance=true;"
providerName="System.Data.SqlClient" />
</connectionStrings>
MSAccessTOMSSQL.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
String connStr = ConfigurationManager.ConnectionStrings["ACCESSdb"].ConnectionString;
String cmdStr = "SELECT * FROM [TableACCESS];";
try
{
using (OleDbConnection conn = new OleDbConnection(connStr))
{
using (OleDbCommand cmd = new OleDbCommand(cmdStr, conn))
{
conn.Open();
using (OleDbDataReader dr = cmd.ExecuteReader())
{
String connStr1 = ConfigurationManager.ConnectionStrings["MSSQLdb"].ConnectionString;
String cmdStr1 = "INSERT INTO [Table1] (col1,col2,col3) VALUES (@col1,@col2,@col3);";
try
{
using (SqlConnection conn1 = new SqlConnection(connStr1))
{
using (SqlCommand cmd1 = new SqlCommand(cmdStr1, conn1))
{
conn1.Open();
cmd1.Parameters.AddWithValue("@col1", dr[1]);
cmd1.Parameters.AddWithValue("@col2", dr[2]);
cmd1.Parameters.AddWithValue("@col3", dr[3]);
cmd1.ExecuteNonQuery();
conn1.Close();
cmd1.Dispose();
conn1.Dispose();
}
}
}
catch (Exception ex)
{
Label2.Text = "Insert Into: " + ex.ToString();
}
}
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
}
catch (Exception ex)
{
Label2.Text = "Access: " + ex.ToString();
}
}