c#怎么使用oledbconnection
1个回答
展开全部
关于使用ADO.NET创建并写入Excel文件与普通数据库操作极为类似,参见以下代码:
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:/test.xls;" +
"Extended Properties=Excel 8.0;";
OleDbConnection cn = new OleDbConnection(sConnectionString);
string sqlCreate = "CREATE TABLE TestSheet ([ID] INTEGER,[Username] VarChar,[UserPwd] VarChar)";
OleDbCommand cmd = new OleDbCommand(sqlCreate, cn);
//创建Excel文件:C:/test.xls
cn.Open();
//创建TestSheet工作表
cmd.ExecuteNonQuery();
//添加数据
cmd.CommandText = "INSERT INTO TestSheet VALUES(1,'elmer','password')";
cmd.ExecuteNonQuery();
//关闭连接
cn.Close();
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:/test.xls;" +
"Extended Properties=Excel 8.0;";
OleDbConnection cn = new OleDbConnection(sConnectionString);
string sqlCreate = "CREATE TABLE TestSheet ([ID] INTEGER,[Username] VarChar,[UserPwd] VarChar)";
OleDbCommand cmd = new OleDbCommand(sqlCreate, cn);
//创建Excel文件:C:/test.xls
cn.Open();
//创建TestSheet工作表
cmd.ExecuteNonQuery();
//添加数据
cmd.CommandText = "INSERT INTO TestSheet VALUES(1,'elmer','password')";
cmd.ExecuteNonQuery();
//关闭连接
cn.Close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询