該代碼將數據從訪問複製到SQL服務器表。 但這段代碼有一些問題。將數據從訪問複製到現有的SQL Server表
- 此代碼無法將數據從訪問權限複製到SQL服務器表所在的數據。
我的SQL服務器表有一些數據,我想從訪問中添加數據到SQL服務器表中的現有數據。
如何將數據添加到現有表中?
- 無法從Access 2007年或2010年
讀取數據如何從訪問讀取數據2007/2010
OpenFileDialog openfiledialog1 = new OpenFileDialog();
openfiledialog1.Title = "select access file";
openfiledialog1.Filter = "Access 2003 (*.mdb)|*.mdb|Access 2007|*.accdb";
if (openfiledialog1.ShowDialog() == DialogResult.OK)
{
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openfiledialog1.FileName;
const string connectionStringDest = @"server=ahmad-pc\anfd;database = phonebook;Integrated Security = true";
using (var sourceConnection = new OleDbConnection(connectionString))
{
sourceConnection.Open();
var commandSourceData = new OleDbCommand("SELECT * from numberperson", sourceConnection);
var reader = commandSourceData.ExecuteReader();
using (var destinationConnection = new SqlConnection(connectionStringDest))
{
destinationConnection.Open();
using (var bulkCopy = new SqlBulkCopy(destinationConnection))
{
bulkCopy.ColumnMappings.Add("name", "nameperson"); //THIS A MAPPING REPLACE IT WITH YOUR NEED
bulkCopy.ColumnMappings.Add("family", "family1");
bulkCopy.DestinationTableName = "profile2";
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}
}
MessageBox.Show("success");
}
}
你應該基於文件的MS-Access版本更改連接字符串。這裏是2007年:http://www.connectionstrings.com/access-2007 – kschieck 2013-04-10 13:07:13