同时插入两表,为啥只执行一个?
我的webservice,本来准备用触发器的,结果因为权限操作问题,不能用触发器,所以只能用两个SQL语句--------------------编程问答-------------------- Mysql的不懂
string result = "";
string sql = "insert into t1(newid title content) values ('" + newid + "','" + title + "','" + content + "')";
string sql1 = "insert into t2(newid,title) values ('" + newid + "','" + title + "')";
MySqlConnection DefSqlCon = new MySqlConnection("server=192.168.0.1;user id=nettest;password=net123;database=test");
DefSqlCon.Open();
MySqlCommand sqlcmd = new MySqlCommand(sql, DefSqlCon);
MySqlCommand sqlcmd1 = new MySqlCommand(sql1, DefSqlCon);
sqlcmd.ExecuteNonQuery();
sqlcmd1.ExecuteNonQuery();
result = "1";
return result;
DefSqlCon.Close();
应该可以写在一起吧
string sql = "insert into t1(newid title content) values ('" + newid + "','" + title + "','" + content + "');insert into t2(newid,title) values ('" + newid + "','" + title + "')";
--------------------编程问答-------------------- sql server支持一次执行多条sql语句
而oracle和ACCESS不支持
LZ的sql不好啊,容易sql注入,传参吧
--------------------编程问答-------------------- 为什么不拼接在一起呢?
两个相关sql语句,也应该用事务处理 --------------------编程问答-------------------- 这语句应该是执行2次 插入两个表的 --------------------编程问答-------------------- 还没一次插入两个表的呢
插入两个表的操作还是放在一个事务里比较好
否则,麻烦比较多 --------------------编程问答-------------------- 事务怎么弄?
不要触发器 --------------------编程问答--------------------
是插入两个表,两个相关连的表 --------------------编程问答--------------------
楼主没必要分成两个SQL语句。放在一起执行,更能提高性能 --------------------编程问答--------------------
呵呵,试了一下,不行 --------------------编程问答--------------------
这两个表,我想用一条SQL语句,可以吗?这样写法不对,第二个表没有插入数据 --------------------编程问答-------------------- sql server支持一次执行多条sql语句
而oracle和ACCESS不支持
LZ的sql不好啊,容易sql注入,传参吧 --------------------编程问答--------------------
string result = "";
string sql = "insert into t1(newid title content) values ('" + newid + "','" + title + "','" + content + "')";
string sql1 = "insert into t2(newid,title) values ('" + newid + "','" + title + "')";
MySqlConnection DefSqlCon = new MySqlConnection("server=192.168.0.1;user id=nettest;password=net123;database=test");
DefSqlCon.Open();
MySqlCommand sqlcmd = new MySqlCommand(sql, DefSqlCon);
sqlcmd.ExecuteNonQuery();
MySqlCommand sqlcmd1 = new MySqlCommand(sql1, DefSqlCon);
sqlcmd1.ExecuteNonQuery();
result = "1";
return result;
DefSqlCon.Close();
--------------------编程问答-------------------- string result = "";
string sql = "insert into t1(newid title content) values ('" + newid + "','" + title + "','" + content + "')";
string sql1 = "insert into t2(newid,title) values ('" + newid + "','" + title + "')";
MySqlConnection DefSqlCon = new MySqlConnection("server=192.168.0.1;user id=nettest;password=net123;database=test");
DefSqlCon.Open();
MySqlCommand sqlcmd = new MySqlCommand(sql, DefSqlCon);
sqlcmd.ExecuteNonQuery();
MySqlCommand sqlcmd1 = new MySqlCommand(sql1, DefSqlCon);
sqlcmd1.ExecuteNonQuery();
DefSqlCon.Close();
result = "1";
return result;
--------------------编程问答-------------------- 可以执行多条语句 --------------------编程问答-------------------- 晕,到现在似乎看明白了:
string result = "";
string sql = "insert into t1(newid title content) values ('" + newid + "','" + title + "','" + content + "')";
string sql1 = "insert into t2(newid,title) values ('" + newid + "','" + title + "')";
MySqlConnection DefSqlCon = new MySqlConnection("server=192.168.0.1;user id=nettest;password=net123;database=test");
DefSqlCon.Open();
MySqlCommand sqlcmd = new MySqlCommand(sql, DefSqlCon);
sqlcmd.ExecuteNonQuery();
DefSqlCon.Close();
MySqlCommand sqlcmd1 = new MySqlCommand(sql1, DefSqlCon);
sqlcmd1.ExecuteNonQuery();
DefSqlCon.Close();
result = "1";
return result; --------------------编程问答--------------------
MySqlCommand sqlcmd = new MySqlCommand(sql, DefSqlCon);
sqlcmd.ExecuteNonQuery();
DefSqlCon.Close();
DefSqlCon.Open();
MySqlCommand sqlcmd1 = new MySqlCommand(sql1, DefSqlCon);
sqlcmd1.ExecuteNonQuery();
DefSqlCon.Close(); --------------------编程问答-------------------- 使用事务吧! --------------------编程问答-------------------- string result = "";
string sql = "insert into t1(newid title content) values ('" + newid + "','" + title + "','" + content + "')";
string sql1 = "insert into t2(newid,title) values ('" + newid + "','" + title + "')";
MySqlConnection DefSqlCon = new MySqlConnection("server=192.168.0.1;user id=nettest;password=net123;database=test");
DefSqlCon.Open();
MySqlCommand sqlcmd = new MySqlCommand(sql, DefSqlCon);
sqlcmd.ExecuteNonQuery();
DefSqlCon.Close();
DefSqlCon.Open();
MySqlCommand sqlcmd1 = new MySqlCommand(sql1, DefSqlCon);
sqlcmd1.ExecuteNonQuery();
DefSqlCon.Close();
result = "1";
return result; --------------------编程问答-------------------- 干脆用储存过程。
传如三参数。
create pro...
insert into t1(newid title content) values (@newid ,@title ,@content)
insert into t2(newid,title) values (@newid ,@title )
--------------------编程问答-------------------- 事物=存储过程? --------------------编程问答--------------------
--------------------编程问答-------------------- 用事物或存储过程~~~~ --------------------编程问答-------------------- 这个是要用到SQL事务来处理的。 --------------------编程问答-------------------- UP --------------------编程问答-------------------- return result;
你先建议一个数据库连接,比如是SQL的连接
SqlConnection sqlcon=new SqlConnection("连接字符串");
sqlcon.open();
SqlTransaction tran = sqlcon.BeginTransaction();
SqlCommand sqlcmd1 = new SqlCommand();
SqlCommand sqlcmd2 = new SqlCommand();
string str1="delete from table1 where id=1";
string str2="delete from table2 where id=2";
try
{
sqlcmd1.CommandText = str1;
sqlcmd1.Connection = sqlcon;
sqlcmd1.Transaction = tran;
sqlcmd1.ExecuteNonQuery();
sqlcmd2.CommandText = str2;
sqlcmd2.Connection = sqlcon;
sqlcmd2.Transaction = tran;
sqlcmd2.ExecuteNonQuery();
tran.commit();//如果两条SQL都执行成功的话,提交事务
}
catch(exception ex)
{
tran.rollback();//如果两条SQL有至少一条没有执行成功,则回滚事务
}
DefSqlCon.Close();
你这数据库关闭能执行到吗?
--------------------编程问答-------------------- 是不是掉“,”号了
insert into t1(newid,title, content) --------------------编程问答--------------------
这是事物?? --------------------编程问答--------------------
是事务 --------------------编程问答-------------------- up
--------------------编程问答--------------------
错误 1 “System.Data.SqlClient.SqlTransaction”不包含“commit”的定义,并且找不到可接受类型为“System.Data.SqlClient.SqlTransaction”的第一个参数的扩展方法“commit”(是否缺少 using 指令或程序集引用?) --------------------编程问答-------------------- 先加个try..catch看看
不行的话,把操作写成一个存储过程拿到sql里调试.
补充:.NET技术 , ASP.NET