このコードは、Access ファイル (.mdb) からテーブル情報を読み取り、テーブル情報を SQL Server テーブルにコピーします。このコードfield1
は、Access ファイルをfield1
SQL Server テーブルにコピーfield2
し、コピー先field2
を ... このコードは正しく機能しますが、変更コピーが必要です。name
Access ファイルからnameperson
SQL Server テーブルのフィールドにフィールドをコピーしたいと考えています。たとえば、SQL Server テーブルfield1
にアクセスしてコピーします。field5
どうすればいいですか?
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");
}
}