简体   繁体   中英

What is the namespace while creating a MS Access database using C#

I am exporting a gridview to MS Access but I am facing some problems.

What namespace I should be using while creating a MS access database in C#?

要使用Microsoft Office认证的文档(例如MS-Access或MS-Excel)进行播放,您需要添加

using System.Data.OleDb

write some code

OleDbConnection conn = new OleDbConnection();

then right click on the class name (eg OleDbConnection) and select resolve or you can use shortcut ctrl + .

In this specific case you need to add

using System.Data.OleDb

You need to add reference to this namespace in your project System.Data.OleDb

and then use it on you file like this

using System.Data.OleDb

The System.Data.OleDb namespace needs to be added in your c# file. You can refer the below detailed sample which help you to learn more about MS Access data storage and retrieval through C# .

Link to Refer

I am using 2 namespace

  1. using ADOX;
  2. Using ADODB;

yes i am using namespace using System.Data.OLEDB; with 2 namespace using ADOX; using ADODB; with this code

    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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM