编写 SQL OleDbCommand 将数据从 SqlServer 导入到 Access 数据库
我有以下代码:
ADOX.Catalog cat = new ADOX.Catalog();
string pathToNewAccessDatabase = "Data Source=D:\\Data\\NewMDB.mdb";
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + pathToNewAccessDatabase + ";Jet OLEDB:Engine Type=5");
System.Data.OleDb.OleDbConnection AccessConn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + pathToNewAccessDatabase);
AccessConn.Open();
System.Data.OleDb.OleDbCommand AccessCommand = new System.Data.OleDb.OleDbCommand("SELECT * INTO [ReportFile] FROM [Data Source=server_path; Initial Catalog=database_name; User Id=user_name; Password=pass_word;Trusted_Connection=False].[dbo.DataSourceTable]", AccessConn);
AccessCommand.ExecuteNonQuery();
AccessConn.Close();
我想从 SQL SERVER
选择到 ACCESS
数据库。
另外,如果密码包含 [
字符,我该如何转义?
I have the following code :
ADOX.Catalog cat = new ADOX.Catalog();
string pathToNewAccessDatabase = "Data Source=D:\\Data\\NewMDB.mdb";
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + pathToNewAccessDatabase + ";Jet OLEDB:Engine Type=5");
System.Data.OleDb.OleDbConnection AccessConn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + pathToNewAccessDatabase);
AccessConn.Open();
System.Data.OleDb.OleDbCommand AccessCommand = new System.Data.OleDb.OleDbCommand("SELECT * INTO [ReportFile] FROM [Data Source=server_path; Initial Catalog=database_name; User Id=user_name; Password=pass_word;Trusted_Connection=False].[dbo.DataSourceTable]", AccessConn);
AccessCommand.ExecuteNonQuery();
AccessConn.Close();
I want to select from SQL SERVER
into the ACCESS
database.
Also, if the password contains the [
character, how do I escape that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议首先建立一个到 SQL 服务器的 SQLConnection 并将所需的数据查询到 DataTable 中。
将数据放入数据表后,通过循环数据来为插入命令创建查询。
您不应转义密码中的任何字符。
I suggest establishing an SQLConnection first to the SQL server and query your desired data into a DataTable.
After you have the data in your datatatable, create the query for your insert command from it by looping through the data.
You should not escape any character from password.