如何通过c#在ms访问中创建表 [英] how to create a table in ms access by c#

查看:67
本文介绍了如何通过c#在ms访问中创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过c#代码创建一个访问表,其中名称可由用户选择,感谢您通知我

i want to create a table in access by c# code where name can be select by user thanks for notice me

推荐答案

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

// Add references for the following COM Objects to the project:
//
//  - Microsoft ActiveX Data Objects 6.0 Library
//  - Microsoft ADO Ext. 6.0 for DDL and Security 
//
using ADOX;
using ADODB;

namespace AccessTest
{
    public partial class Form1 : Form
    {
        const string _ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=BookCSharp.mdb";

        private Catalog OpenDatabase()
        {
            Catalog catalog = new Catalog();
            Connection connection = new Connection();
        
            try
            {
                connection.Open( _ConnectionString);
                catalog.ActiveConnection = connection;
            }
            catch (Exception)
            {
                catalog.Create(_ConnectionString);
            }
            return catalog;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            // Only for demonstration purposes, no error checks:
            // This code will only work as long as the table "Publisher" does not exist

            // First create an new database if necessary
            Catalog cat = OpenDatabase();

            // Create a new table "Publisher" using ADOX ...
            Table table = new Table();
            table.Name = "Publisher";
            cat.Tables.Append(table);

            // Add Column "PublisherID" with Autoincrement
            ADOX.Column col = new Column();
            col.Name = "PublisherID";
            col.ParentCatalog = cat;
            col.Type = ADOX.DataTypeEnum.adInteger;
            col.Properties["Nullable"].Value = false;
            col.Properties["AutoIncrement"].Value = true;
            table.Columns.Append(col);

            // Add column "PublisherName"
            col = new Column();
            col.Name = "PublisherName";
            col.ParentCatalog = cat;
            col.Type = ADOX.DataTypeEnum.adWChar;
            col.DefinedSize = 50;
            col.Attributes = ColumnAttributesEnum.adColNullable;
            table.Columns.Append(col);

            // Make "PublisherID" the primary key
            ADOX.Index index = new ADOX.Index();
            index.PrimaryKey = true;
            index.Name = "PK_Publisher";
            index.Columns.Append("PublisherID", table.Columns["PublisherID"].Type, table.Columns["PublisherID"].DefinedSize);
            table.Indexes.Append(index);

            MessageBox.Show("A new Data Table is successfully Created");

        }

        public Form1()
        {
            InitializeComponent();
        }

    }


}







希望有所帮助,

Sharath kumar




Hope that helps,
Sharath kumar


试试这个: -



try this:-

// Creating OLEDB connection string for Ms-Access 2007 database file
            OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= D:\\MyDatabase.accdb;Persist Security Info=False;");
            myConnection.Open();
            // Create Oledb command to execute particular query
            OleDbCommand myCommand = new OleDbCommand();
            myCommand.Connection = myConnection;
            // Query to create table with specified data columne
            myCommand.CommandText = "CREATE TABLE tblIdentityTesting([MyIdentityColumn] long, [Name] text)";
            myCommand.ExecuteNonQuery();
            MessageBox.Show("Table Created Successfully");





如果您使用ms-access 2003然后更改



if you using ms-access 2003 then change

OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source= D:\\MyDatabase1.mdb; OLE DB Services=-1");


这篇关于如何通过c#在ms访问中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆