编写 SQL OleDbCommand 将数据从 SqlServer 导入到 Access 数据库

发布于 2024-12-27 18:50:52 字数 860 浏览 1 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断肠人 2025-01-03 18:50:52

我建议首先建立一个到 SQL 服务器的 SQLConnection 并将所需的数据查询到 DataTable 中。

using (SqlConnection conn = new SqlConnection("yourConnectionString"))
{
    using (SqlCommand comm = new SqlCommand("Select columns from targetTable", conn))
    {                    
        SqlDataReader reader = comm.ExecuteReader();
        DataTable tbl = new DataTable();
        tbl.Load(reader);
    }
}

将数据放入数据表后,通过循环数据来为插入命令创建查询。

string insertCommandString = string.Empty;

for (int row = 0; row < tbl.Rows.Count; row++)
{
    insertCommandString = "Insert into yourTableName(yourColumnNames) values(";
    for (int column = 0; column < tbl.Columns.Count; column++)
    {
        if(tbl.Columns[column].DataType == typeof(String))
        {
            insertCommandString += "'" + tbl.Rows[row][column].ToString() + "'";
        }
        else
        {
            insertCommandString += tbl.Rows[row][column].ToString();
        }

        if (column < tbl.Columns.Count - 1)
        {
            insertCommandString += ",";
        }
   }

   insertCommandString += ")";

   System.Data.OleDb.OleDbCommand AccessCommand = new System.Data.OleDb.OleDbCommand(insertCommandString, AccessConn);
   AccessCommand.ExecuteNonQuery();
 }

您不应转义密码中的任何字符。

I suggest establishing an SQLConnection first to the SQL server and query your desired data into a DataTable.

using (SqlConnection conn = new SqlConnection("yourConnectionString"))
{
    using (SqlCommand comm = new SqlCommand("Select columns from targetTable", conn))
    {                    
        SqlDataReader reader = comm.ExecuteReader();
        DataTable tbl = new DataTable();
        tbl.Load(reader);
    }
}

After you have the data in your datatatable, create the query for your insert command from it by looping through the data.

string insertCommandString = string.Empty;

for (int row = 0; row < tbl.Rows.Count; row++)
{
    insertCommandString = "Insert into yourTableName(yourColumnNames) values(";
    for (int column = 0; column < tbl.Columns.Count; column++)
    {
        if(tbl.Columns[column].DataType == typeof(String))
        {
            insertCommandString += "'" + tbl.Rows[row][column].ToString() + "'";
        }
        else
        {
            insertCommandString += tbl.Rows[row][column].ToString();
        }

        if (column < tbl.Columns.Count - 1)
        {
            insertCommandString += ",";
        }
   }

   insertCommandString += ")";

   System.Data.OleDb.OleDbCommand AccessCommand = new System.Data.OleDb.OleDbCommand(insertCommandString, AccessConn);
   AccessCommand.ExecuteNonQuery();
 }

You should not escape any character from password.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文