首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用列表C#填充由ADOX创建的列

用列表C#填充由ADOX创建的列
EN

Stack Overflow用户
提问于 2015-02-17 10:53:09
回答 1查看 1K关注 0票数 0

我用ADOX在数据库中创建了一个表。我希望用列表填充我的列。我该怎么做?一个列表是一个应该填充"ScheduleName“列的字符串,一个列表是应该填充"SchedulePace”列的整数列表。下面是我如何创建我的表:

代码语言:javascript
复制
public partial class ScheduleStart : Form
{
    const string _ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\A2 Computing\C# Programming Project\TriHard.accdb";

    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 = "ScheduleEvent";
        cat.Tables.Append(table);

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

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

        // Add column "SchedulePace"
        col = new Column();
        col.Name = "SchedulePace";
        col.ParentCatalog = cat;
        col.Type = ADOX.DataTypeEnum.adInteger;
        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_ScheduleEvent";
        index.Columns.Append("ScheduleID", table.Columns["ScheduleID"].Type, table.Columns["ScheduleID"].DefinedSize);
        table.Indexes.Append(index);

        MessageBox.Show("The Schedule table has been created");

    }

    public ScheduleStart()
    {
        InitializeComponent();
    }

因此,这将创建表,主键在我添加新值时自动递增。我希望用我创建的列表填充其他两个字段。我该怎么做?

EN

回答 1

Stack Overflow用户

发布于 2015-02-17 14:15:16

创建表后,可以使用System.Data.OleDbSystem.Data.Odbc打开.Connection,然后使用准备好的语句(参数化查询)

代码语言:javascript
复制
INSERT INTO ScheduleEvent (ScheduleName, SchedulePace) VALUES (?, ?)

这里有很多关于堆栈溢出的例子,所以您应该不会有任何困难找到一个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28560300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档