C# access文件表和sql server表之间的关系
此代码从Access文件(.mdb)读取表信息,并将表信息复制到sql server表。 此代码将Access文件中的C# access文件表和sql server表之间的关系,c#,sql,winforms,C#,Sql,Winforms,此代码从Access文件(.mdb)读取表信息,并将表信息复制到sql server表。 此代码将Access文件中的field1复制到sql server表中的field1,并将field2复制到field2,然后。。。 此代码工作正常,但我希望更改副本。 我想将Access文件中的name字段复制到sql server表中的nameperson字段。 例如,在访问sql server表中的field5时复制field1。 我该怎么做 OpenFileDialog openfiledialo
field1
复制到sql server表中的field1
,并将field2
复制到field2
,然后。。。
此代码工作正常,但我希望更改副本。
我想将Access文件中的name
字段复制到sql server表中的nameperson
字段。
例如,在访问sql server表中的field5
时复制field1
。
我该怎么做
OpenFileDialog openfiledialog1 = new OpenFileDialog();
openfiledialog1.Title = "select path access file";
openfiledialog1.Filter = "Access 2003 (*.mdb)|*.mdb";
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 id , name , family from numberperson", sourceConnection);
var reader = commandSourceData.ExecuteReader();
using (var destinationConnection = new SqlConnection(connectionStringDest))
{
destinationConnection.Open();
using (var bulkCopy = new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName = "profile2";
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}
}
MessageBox.Show("copy successully");
}
}
请参见下面的示例,我为您添加了一个将列名转换为nameperson的映射
[...]
using (var bulkCopy = new SqlBulkCopy(destinationConnection))
{
bulkCopy.ColumnMappings.Add("name", "nameperson"); //THIS A MAPPING REPLACE IT WITH YOUR NEED
bulkCopy.DestinationTableName = "profile2";
[....]
谢谢现在将名称字段复制到sqlserver中的nameperson字段。