0

我的问题是如何在 C# 中将 gridview 导出到 MS Access 中,为此我使用以下代码:

String accessConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\...\\test.accdb;";
String sqlConnectionString = ConfigurationManager.ConnectionStrings["College"].ConnectionString;

    //Make adapters for each table we want to export
    SqlDataAdapter adapter1 = new SqlDataAdapter();
  //  SqlDataAdapter adapter2 = new SqlDataAdapter();
    DataTable dtFillGrid = (DataTable)ViewState["FillGrid"];
    //Fills the data set with data from the SQL database
   // DataSet dataSet = new DataSet();
    adapter1.Fill(dtFillGrid);
  //  adapter2.Fill(dataSet, "Table2");

    //Create an empty Access file that we will fill with data from the data set
    ADOX.Catalog catalog = new ADOX.Catalog();
    catalog.Create(accessConnectionString);

    //Create an Access connection and a command that we'll use
    OleDbConnection accessConnection = new OleDbConnection(accessConnectionString);
    OleDbCommand command = new OleDbCommand();
    command.Connection = accessConnection;
    command.CommandType = CommandType.Text;
    accessConnection.Open();

    //This loop creates the structure of the database
    foreach (DataTable table in dtFillGrid.Rows)
    {
        String columnsCommandText = "(";
        foreach (DataColumn column in table.Columns)
        {
            String columnName = column.ColumnName;
            String dataTypeName = column.DataType.Name;
            String sqlDataTypeName = getSqlDataTypeName(dataTypeName);
            columnsCommandText += "[" + columnName + "] " + sqlDataTypeName + ",";
        }
        columnsCommandText = columnsCommandText.Remove(columnsCommandText.Length - 1);
        columnsCommandText += ")";

        command.CommandText = "CREATE TABLE " + table.TableName + columnsCommandText;

        command.ExecuteNonQuery();
    }

    //This loop fills the database with all information
    foreach (DataTable table in dtFillGrid.Rows)
    {
        foreach (DataRow row in table.Rows)
        {
            String commandText = "INSERT INTO " + table.TableName + " VALUES (";
            foreach (var item in row.ItemArray)
            {
                commandText += "'" + item.ToString() + "',";
            }
            commandText = commandText.Remove(commandText.Length - 1);
            commandText += ")";

            command.CommandText = commandText;
            command.ExecuteNonQuery();
        }
    }

    accessConnection.Close();

但在这段代码中我得到一个错误:

找不到类型或命名空间名称 ADOX

4

3 回答 3

0

根据 accessConnectionString 内容,您应该在您的机器上安装“数据连接组件”。可从http://www.microsoft.com/en-us/download/details.aspx?id=23734下载

于 2013-07-20T12:27:22.497 回答
0

您需要添加参考Microsoft ADO Ext. 2.7 for DDL and Security(版本号可能不同),可以在参考对话框的 COM 部分下找到,然后添加using ADOX;您的代码。

于 2013-07-20T12:27:29.923 回答
0

在这里,我没有在 MS Access 中导出 gridview,但我在此代码的帮助下使用 C# 在 MS Access 中创建了一个数据库

    ADOX.Catalog cat = new ADOX.Catalog();
    ADOX.Table table = new ADOX.Table();

    //Create the table and it's fields. 
    table.Name = "Table1";
    table.Columns.Append("PartNumber", ADOX.DataTypeEnum.adVarWChar, 6); // text[6]
    table.Columns.Append("AnInteger", ADOX.DataTypeEnum.adInteger, 10); // Integer 
    try
    {
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=d:/m2.accdb;" + "Jet OLEDB:Engine Type=5");
        cat.Tables.Append(table);
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" + "Data Source=d:/m2.accdb");
       conn.Open();

             OleDbCommand cmd = new OleDbCommand();
             cmd.Connection = conn;

             cmd.CommandText = "INSERT INTO Table1([PartNumber],[AnInteger]) VALUES (@FirstName,@LastName)";

             cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = "neha";

             cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = 20;

             cmd.ExecuteNonQuery();

            conn.Close();                                 

    }
    catch (Exception ex)
    {
        result = false;
    }
    cat = null;
于 2013-07-25T05:50:26.320 回答