3个回答
展开全部
在存储过程中写两个INSERT 就可以了
create proc up_insert
as
begin
insert into test valuse(1);
insert into test values(2);
end;
create proc up_insert
as
begin
insert into test valuse(1);
insert into test values(2);
end;
更多追问追答
追问
但是我插入的 是 不同的数据, 参数怎么写啊!!!!
追答
create proc up_insert
@a int,
@b int
as
begin
insert into test valuse(@a);
insert into test values(@b);
end;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
public static void ExecuteSqlTran(Hashtable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText=myDE.Key.ToString();
SqlParameter[] cmdParms=(SqlParameter[])myDE.Value;
PrepareCommand(cmd,conn,trans,cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
trans.Commit();
}
}
catch
{
trans.Rollback();
throw;
}
}
}
}
eg:
Hashtable SQLStringList = new Hashtable();
//循环开始
string cmdText = "insert into userinfo (username,userpwd)values(@username,@userpwd) ";
SqlParameter[] cmdParms = new SqlParameter[2];
cmdParms[0] = new SqlParameter( "@username ", "name ");
cmdParms[1] = new SqlParameter( "@userpwd ", "pwd ");
....
SQLStringList.Add(cmdText, cmdParms);
//循环结束
ExecuteSqlTran(SQLStringList);
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
public static void ExecuteSqlTran(Hashtable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
SqlCommand cmd = new SqlCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText=myDE.Key.ToString();
SqlParameter[] cmdParms=(SqlParameter[])myDE.Value;
PrepareCommand(cmd,conn,trans,cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
trans.Commit();
}
}
catch
{
trans.Rollback();
throw;
}
}
}
}
eg:
Hashtable SQLStringList = new Hashtable();
//循环开始
string cmdText = "insert into userinfo (username,userpwd)values(@username,@userpwd) ";
SqlParameter[] cmdParms = new SqlParameter[2];
cmdParms[0] = new SqlParameter( "@username ", "name ");
cmdParms[1] = new SqlParameter( "@userpwd ", "pwd ");
....
SQLStringList.Add(cmdText, cmdParms);
//循环结束
ExecuteSqlTran(SQLStringList);
追问
就像 有 两个 插入sql语句 ,一个 插入一条数据,另一个 插入多条数据 该怎么办
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |