代码如下:
string connstr = "server=.;database=library;uid=sa;pwd=foolish;";
SqlConnection conn = new SqlConnection(connstr);
string sqlstr2 = "update book set 图书数量=图书数量-" + textBox5.Text + " where 图书编号=" + textBox4.Text;
string sqlstr = "update book2 set 借阅数量=借阅数量+" + textBox5.Text + " where 被借阅图书编号=" + textBox1.Text ;
SqlCommand comm=new SqlCommand(sqlstr,conn);
SqlCommand comm2 = new SqlCommand(sqlstr2, conn);
conn.Open();
SqlDataReader r = comm.ExecuteReader();
if (r.Read() == true)
{
MessageBox.Show("归还成功");
}
string connstr = "server=.;database=library;uid=sa;pwd=foolish;";
SqlConnection conn = new SqlConnection(connstr);
string sqlstr2 = "update book set 图书数量=图书数量-" + textBox5.Text + " where 图书编号=" + textBox4.Text;
string sqlstr = "update book2 set 借阅数量=借阅数量+" + textBox5.Text + " where 被借阅图书编号=" + textBox1.Text ;
SqlCommand comm=new SqlCommand(sqlstr,conn);
SqlCommand comm2 = new SqlCommand(sqlstr2, conn);
conn.Open();
SqlDataReader r = comm.ExecuteReader();
if (r.Read() == true)
{
MessageBox.Show("归还成功");
}
谢谢!
string sqlstr = "update book2 set [借阅数量]=[借阅数量]+" + Convert.Int32(textBox5.Text.Trim()) + " where 被借阅图书编号=" + textBox1.Text ;
说明连接数据库是正常的,那就是你写的sql语句有问题,
单步跟踪下 你的sql,如果没有发现问题 就复制该sql到查询分析器里 ,
让它执行一下 看看问题出在哪儿!!!
SqlConnection conn = new SqlConnection(connstr);
string sqlstr2 = "update book set 图书数量=图书数量+" + Convert.ToInt32(textBox2.Text.Trim()) + " where 图书编号=" + Convert.ToInt32(textBox1.Text.Trim());
string sqlstr = "update book2 set 借阅数量=借阅数量-" + Convert.ToInt32(textBox2.Text.Trim()) + " where 被借阅图书编号=" + Convert.ToInt32(textBox1.Text.Trim());
SqlCommand comm = new SqlCommand(sqlstr, conn);
SqlCommand comm2 = new SqlCommand(sqlstr2, conn);
conn.Open();
SqlDataReader r = comm.ExecuteReader();
if (r.Read() == true)
{
MessageBox.Show("归还成功");
}改成这样以后 双击借阅按钮木有用了。
正确的如下:
string sqlstr2 = "update book set 图书数量='"+"图书数量-" + textBox5.Text + "' where 图书编号='" + textBox4.Text+"'";
图书编号如果是整型则可加可不加,如果是字符串型的则必须加。
int huanshu=Convet.Toint32(textbox.text); string connstr = "server=.;database=library;uid=sa;pwd=foolish;";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string sqlstr2 = "update book set 图书数量=图书数量-" + huanshu + " where 图书编号=" + textBox4.Text;
string sqlstr = "update book2 set 借阅数量=借阅数量+" + huanshu + " where 被借阅图书编号=" + textBox1.Text ;
SqlCommand comm=new SqlCommand(sqlstr,conn);
SqlCommand comm2 = new SqlCommand(sqlstr2, conn); if (comm.ExecuteNonQuery()>0)
{
MessageBox.Show("归还成功");
顺序反了,还有需要转换字符串,update 返回的是受影响的行数试试这个!
}
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string sqlstr2 = "update book set 图书数量=图书数量+" + Convert.ToInt32(textBox2.Text) + " where 图书编号=" + Convert.ToInt32(textBox1.Text);
string sqlstr = "update book2 set 借阅数量=借阅数量-" + Convert.ToInt32(textBox2.Text) + " where 被借阅图书编号=" + Convert.ToInt32(textBox1.Text);
//string sqlstr2 = "update book set 图书数量=图书数量+" + textBox2.Text + " where 图书编号=" + textBox1.Text ;
//string sqlstr = "update book2 set 借阅数量=借阅数量-" + textBox2.Text + " where 被借阅图书编号=" + textBox1.Text ;
SqlCommand comm = new SqlCommand(sqlstr, conn);
SqlCommand comm2 = new SqlCommand(sqlstr2, conn);
if (comm.ExecuteNonQuery() > 0)
{
MessageBox.Show("归还成功");
}感觉差别不是很大 归还成功的消息框 还是没弹出来
string sqlstr = "update book2 set 借阅数量=借阅数量-cast("+textBox2.Text+' as int) where 被借阅图书编号=" + Convert.ToInt32(textBox1.Text);这样你试试!
if (comm.ExecuteNonQuery() > 0 &&comm2.ExecuteNonQuery() > 0)
{
MessageBox.Show("归还成功");
}
有两个SqlCommand 对象,只有两个都成功的时候才归还成功~!记得关闭连接如果还没有成功,你调试自己看一下!
SqlConnection conn = new SqlConnection(connstr);
//string sqlstr2 = "update book set 图书数量=图书数量-" + textBox5.Text + " where 图书编号=" + textBox4.Text;
string sqlstr2 = "update book set 图书数量=图书数量-" + Convert.ToInt32(textBox5.Text) + " where 图书编号=" + textBox4.Text;
string sqlstr = "update book2 set 借阅数量=借阅数量+" + Convert.ToInt32(textBox5.Text) + " where 被借阅图书编号=" + textBox4.Text;
conn.Open(); if (comm.ExecuteNonQuery() > 0 && comm2.ExecuteNonQuery() > 0)
{
MessageBox.Show("借阅成功");
}怎么把两句 SqlCommand comm=new SqlCommand(sqlstr,conn);
SqlCommand comm2 = new SqlCommand(sqlstr2, conn2);
合并到一句 求答案 给分!!
SqlCommand comm2=new SqlCommand(sqlstr2,conn);
合并到一起
把两个sql语句连接到一起,或者执行两次ExecuteNonQuery就好了
你是在说我吗?
我想请教下哪一点误人子弟了,请不吝赐教。string sqlstr2 = "update book set 图书数量='"+"图书数量-" + textBox5.Text + "' where 图书编号='" + textBox4.Text+"'";
图书编号如果是整型则可加可不加,如果是字符串型的则必须加。
能不能帮我解决两句
SqlCommand comm=new SqlCommand(sqlstr,conn);
SqlCommand comm2=new SqlCommand(sqlstr2,conn);
合并问题
SqlConnection conn = new SqlConnection(connstr);
string sqlstr2 = "update book set 图书数量=图书数量-" + Convert.ToInt32(textBox5.Text) + " where 图书编号=" + textBox4.Text;
string sqlstr = "update book2 set 借阅数量=借阅数量+" + Convert.ToInt32(textBox5.Text) + " where 被借阅图书编号=" + textBox4.Text;
SqlCommand comm=new SqlCommand(sqlstr,conn);
SqlCommand comm2 = new SqlCommand(sqlstr2, conn);
conn.Open();
if (comm.ExecuteNonQuery() > 0 && comm2.ExecuteNonQuery() > 0)
{
MessageBox.Show("借阅成功");
}
因为我写了两句 string SQL 语句如果单单运行一句SqlCommand comm=new SqlCommand(sqlstr,conn);
可以运行成功 但是运行两句就有冲突 或者没有反应
怎么才能把两句
SqlCommand comm=new SqlCommand(sqlstr,conn);
SqlCommand comm2=new SqlCommand(sqlstr2,conn);
合并在一起
看看你的sql语句吧。