sql数据库:
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
con.Open();
string sql="";
sql += "insert into userinfo (num,name) values(1,'a')";
sql += "insert into userinfo (num,name) values(2,'b')";
sql += "insert into userinfo (num,name) values(3,'c')";
SqlCommand cmd = new SqlCommand(sql, con);
if (cmd.ExecuteNonQuery()>0)
{
MessageBox.Show("OK");
}
Access数据库:
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=d:\\GYMRace.mdb");
con.Open();
string sql = "";
sql += "insert into teaminfo (num,username,isplay) values(1,'a',1)";
sql += "insert into teaminfo (num,username,isplay) values(2,'b',1)";
sql += "insert into teaminfo (num,username,isplay) values(3,'c',1)";
OleDbCommand cmd = new OleDbCommand(sql, con);
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("OK");
}
执行第一个没有错误。执行第二个就有错误,说Sql语句的结束位置缺少分号。当我在每条sql语句的结尾加上分号的时候,又说在sql语句结尾之后找到字符,把insert前面加一个空格也不行。是不是不能向Access数据库一次插入多条数据啊,请大家帮我看看,谢谢了。
8 个解决方案
#1
按照你说的,access应该是不可以吧,换个方法做呗,放在一个事物中处理。。。
#2
分号是肯定要加的。
#3
顶下
#4
是半角分号,你该不会写是全角的了吧。
#5
没有用过Access数据库,关注下
#6
顶一下
#7
路径?
#8
access不支持上面这种用法,可以用事务循环插入多行数据:
/*=============2009/06/09 测试通过===============*/
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:/Inetpub/wwwroot/DotNetArticle/App_Data/Article.mdb"); //这里换成你的路径
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.Transaction = con.BeginTransaction();
try
{
string sql1 = "insert into teaminfo (num,username,isplay) values(1,'a',1)";
string sql2 = "insert into teaminfo (num,username,isplay) values(2,'b',1)";
string sql3 = "insert into teaminfo (num,username,isplay) values(3,'c',1)";
string[] SQLStringList = { sql1, sql2, sql3 };
for (int n = 0; n < SQLStringList.Length; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
cmd.Transaction.Commit(); //提交事务
MessageBox.Show("OK");
}
catch (Exception)
{
cmd.Transaction.Rollback();
}
finally
{
con.Close();
}
#1
按照你说的,access应该是不可以吧,换个方法做呗,放在一个事物中处理。。。
#2
分号是肯定要加的。
#3
顶下
#4
是半角分号,你该不会写是全角的了吧。
#5
没有用过Access数据库,关注下
#6
顶一下
#7
路径?
#8
access不支持上面这种用法,可以用事务循环插入多行数据:
/*=============2009/06/09 测试通过===============*/
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:/Inetpub/wwwroot/DotNetArticle/App_Data/Article.mdb"); //这里换成你的路径
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.Transaction = con.BeginTransaction();
try
{
string sql1 = "insert into teaminfo (num,username,isplay) values(1,'a',1)";
string sql2 = "insert into teaminfo (num,username,isplay) values(2,'b',1)";
string sql3 = "insert into teaminfo (num,username,isplay) values(3,'c',1)";
string[] SQLStringList = { sql1, sql2, sql3 };
for (int n = 0; n < SQLStringList.Length; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
cmd.Transaction.Commit(); //提交事务
MessageBox.Show("OK");
}
catch (Exception)
{
cmd.Transaction.Rollback();
}
finally
{
con.Close();
}