代码如下:
 
             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("归还成功");
             }

解决方案 »

  1.   

    这只是更新不要用reader使用Scalar()什么的,就行了.
      

  2.   

    拼接sql不用转,转就错了转换函数是 Convert.ToInt32("123")
      

  3.   

    能具体点吗? scalar() 怎么写  写在哪?
    谢谢!
      

  4.   

     改成这样试试
    string sqlstr = "update book2 set [借阅数量]=[借阅数量]+" + Convert.Int32(textBox5.Text.Trim()) + " where 被借阅图书编号=" + textBox1.Text ;  
      

  5.   

    能执行到 SqlDataReader r = comm.ExecuteReader();
    说明连接数据库是正常的,那就是你写的sql语句有问题,
    单步跟踪下 你的sql,如果没有发现问题  就复制该sql到查询分析器里 ,
    让它执行一下  看看问题出在哪儿!!!
      

  6.   

      string connstr = "server=.;database=library;uid=sa;pwd=foolish;";
                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("归还成功");
                }改成这样以后 双击借阅按钮木有用了。
      

  7.   

    数字不用转,但是语句中必须加单引号,否则肯定错。
    正确的如下:
    string sqlstr2 = "update book set 图书数量='"+"图书数量-" + textBox5.Text + "' where 图书编号='" + textBox4.Text+"'";
    图书编号如果是整型则可加可不加,如果是字符串型的则必须加。
      

  8.   


    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 返回的是受影响的行数试试这个!
      }
      

  9.   

    参考这个写法 还有 在 C# 中 数字 不要 加引号 LS 误人子弟注意下 
      

  10.   

    string connstr = "server=.;database=library;uid=sa;pwd=foolish;";
                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("归还成功");
                }感觉差别不是很大 归还成功的消息框 还是没弹出来
      

  11.   

     string sqlstr2 = "update book set 图书数量=图书数量+ cast("+textBox2.Text+" as int)  where 图书编号=" + Convert.ToInt32(textBox1.Text);
      string sqlstr = "update book2 set 借阅数量=借阅数量-cast("+textBox2.Text+' as int)  where 被借阅图书编号=" + Convert.ToInt32(textBox1.Text);这样你试试!
      

  12.   

        
      if (comm.ExecuteNonQuery() > 0 &&comm2.ExecuteNonQuery() > 0)
      {
      MessageBox.Show("归还成功");
      }
    有两个SqlCommand 对象,只有两个都成功的时候才归还成功~!记得关闭连接如果还没有成功,你调试自己看一下!
      

  13.   

      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 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);
    合并到一句  求答案 给分!!
      

  14.   

    单单运行一个SqlCommand comm=new SqlCommand(sqlstr,conn);成功了 现在的问题是 怎么把两句SqlCommand comm=new SqlCommand(sqlstr,conn);
    SqlCommand comm2=new SqlCommand(sqlstr2,conn);
    合并到一起
      

  15.   

    楼上你是想点击的同时实现图书数量和借阅数量的更新吗?需要写两个方法吧。。这样同时执行两个SQL语句可以的么?疑惑中。
      

  16.   


    把两个sql语句连接到一起,或者执行两次ExecuteNonQuery就好了
      

  17.   


    你是在说我吗?
    我想请教下哪一点误人子弟了,请不吝赐教。string sqlstr2 = "update book set 图书数量='"+"图书数量-" + textBox5.Text + "' where 图书编号='" + textBox4.Text+"'";
    图书编号如果是整型则可加可不加,如果是字符串型的则必须加。
      

  18.   

    图书数量="+"图书数量-" + textBox5.Text + " 不解释
      

  19.   

    好吧,这挺简单,去SQL运行下 没问题
    能不能帮我解决两句
    SqlCommand comm=new SqlCommand(sqlstr,conn);
    SqlCommand comm2=new SqlCommand(sqlstr2,conn);
    合并问题
      

  20.   

    string connstr = "server=.;database=library;uid=sa;pwd=foolish;";
                 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);
    合并在一起 
      

  21.   

    sqldatareader   reader 啊   怎么去"update"
      

  22.   

    真是服了lz 了,你是想要借书和还书一起执行吗?那你写两个不就行了,然后判断受影响行数,至于你说的没弹出证明你的sql 语句执行以后了,受影响的行数是0,所以没弹出。
    看看你的sql语句吧。
      

  23.   

    那你为什么不用变量接收文本框的值,然后在转换int,这样你接收时可以判断文本框是否为空等待,也可以限制文本框只能输入数字,再加上正确的sql就ok了
      

  24.   

    借书,还书本来就不是同时执行的吧,就算是借了一本书,同时还另一本书也是该分逻辑来写的。楼主写个sp多简单啊,传本次借书,还书的数量,图书编号就ok了,干嘛非要拼sql语句,浪费力气。你debug一下那个sql语句然后再sql查询分析器执行一下不就知道sql有没有问题了么?