我在ADOX的数据库中创建了一个表。我希望用列表填充我的列。我怎样才能做到这一点?一个列表是一个字符串,应该填充“ScheduleName”列,一个列表是一个整数列表,应该填充“SchedulePace”列。以下是我创建表格的方法:
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();
}
因此,当我添加新值时,这会创建表并且主键会自行递增。我希望用我创建的列表填充其他两个字段。我怎么能这样做?
答案 0 :(得分:0)
创建表格后,您可以使用System.Data.OleDb
或System.Data.Odbc
打开.Connection
,然后使用预准备语句(参数化查询)
INSERT INTO ScheduleEvent (ScheduleName, SchedulePace) VALUES (?, ?)
Stack Overflow上有很多示例,所以你找不到任何问题。