当我们写数据库操作的时候,第次都只写一句SQL,能不能同时写两句呢?
例如:
string cmdstr="insert into table (id) values('1');update table set id=2";
SqlConnection cnn="server=(local)....";
SqlCommand cmd=new SqlCommand(cmdstr,cnn);
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
例如:
string cmdstr="insert into table (id) values('1');update table set id=2";
SqlConnection cnn="server=(local)....";
SqlCommand cmd=new SqlCommand(cmdstr,cnn);
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
解决方案 »
- 这个网页如何设计?
- 大家推荐一个适合学校用的多用户博客
- 我想在自己的网站上开发一个彩铃业务系统,要实现的功能包括:客户可以根据………
- hhfeditor,不知道如何读取里面的内容
- webchart 如何根据数据设置柱型的颜色
- 如何将数据库存入的时间每隔24小时更新在Web上实现?拜托各位啊!
- .net输出Excel时中文出现乱码
- 关于自定义控件的问题,希望高手给于解决!谢谢了,分不够在开100!
- 讲讲Page_Init,Page_Load的作用,区别
- 我用hash算法加密了一个用户口令,可是怎么才能把它写进数据库?
- ======= GridView 中能不能把其中一列提行显示???==============
- .asx播放列表 为什么有的机器不能播???
然后用DATASET全收,有多少条查询记录就有多少个table
这样也可以,SQL语句后面加上;
SqlConnection cnn="server=(local)....";
SqlCommand cmd=new SqlCommand();
cnn.Open();
cmd.connection=con;
cmd.commandtext="insert into table (id) values('1')";
cmd.ExecuteNonQuery();
cmd.comandtext="update table set id=2";
cmd.ExecuteNonQuery();
cnn.Close();
public void oiSave()
{
OleDbConnection con = DBConnection.createCon();
con.Open();
OleDbCommand cmd = new OleDbCommand("",con);
cmd.CommandText += "insert into OutIn values(@No,@ProNo,@Type,@Count,@Leavings,@Price,@Time,@Location,@DoNo)";
cmd.Parameters.Add("@No",this.No);
cmd.Parameters.Add("@ProNo",this.ProNo);
cmd.Parameters.Add("@Type",this.Type);
cmd.Parameters.Add("@Count",this.Count);
cmd.Parameters.Add("@Leavings",this.Count);
cmd.Parameters.Add("@Price",this.Price);
cmd.Parameters.Add("@Time",this.Time);
cmd.Parameters.Add("@Location",this.Location);
cmd.Parameters.Add("@DoNo",this.DoNo);
OleDbTransaction odt = con.BeginTransaction();
cmd.Transaction = odt;
try
{
if(this.Type=="入库")
{
cmd.ExecuteNonQuery();
cmd.CommandText = "update Products set ProCount=ProCount+"+this.Count+" where ProNo='"+this.ProNo+"'";
cmd.ExecuteNonQuery();
odt.Commit();
}
else if(this.Type=="出库")
{
cmd.ExecuteNonQuery();
cmd.CommandText = "update OutIn set Leavings=Leavings-"+this.Count+" where No='"+this.DoNo+"'";
cmd.ExecuteNonQuery();
cmd.CommandText = "update Products set ProCount=ProCount-"+this.Count+" where ProNo='"+this.ProNo+"'";
cmd.ExecuteNonQuery();
odt.Commit();
}
}
catch(OleDbException ex)
{
odt.Rollback();
throw ex;
}
finally
{
con.Close();
}
}