用c#编写,创建一个access数据库Exam.mdb,在其中创建表student,字段(Num,N 20
用c#编写,创建一个access数据库Exam.mdb,在其中创建表student,字段(Num,Name,Birthday),填写若干记录。...
用c#编写,创建一个access数据库Exam.mdb,在其中创建表student,字段(Num,Name,Birthday),填写若干记录。
展开
1个回答
展开全部
先在程序中添加对Microsoft ADO Ext 2.8的引用,然后
string db = Application.StartupPath + @"\Exam.mdb";
string connstr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", db);
ADOX.Catalog catalog = new Catalog();
catalog.Create(connstr);//创建数据库
using (OleDbConnection connection = new OleDbConnection(connstr))
{
connection.Open();
OleDbCommand cmd = connection.CreateCommand();
cmd.CommandText = "CREATE TABLE student(Num AUTOINCREMENT,Name TEXT(50),Birthday DateTime,CONSTRAINT table1_PK PRIMARY KEY(Num));";
cmd.ExecuteNonQuery();//创建表
//插入三条数据
string insert = "insert into student(Name,Birthday) values('{0}',#{1}#)";
cmd.CommandText = string.Format(insert,"MARY", "2016-02-14 22:04:36");
cmd.ExecuteNonQuery();
cmd.CommandText = string.Format(insert, "PATRICIA", "2015-02-20 18:05:27");
cmd.ExecuteNonQuery();
cmd.CommandText = string.Format(insert, "LINDA", "2013-10-18 05:03:28");
cmd.ExecuteNonQuery();
connection.Close();
}
//关闭数据库
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(catalog.ActiveConnection);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(catalog);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询