日期:2014-05-18  浏览次数:21111 次

c# textbox.text 怎么转换为数字 或者 SqlDataReader r = comm.ExecuteReader(); "=" 处为什么出错
代码如下:
 
  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 ;
------解决方案--------------------
能执行到 SqlDataReader r = comm.ExecuteReader();
说明连接数据库是正常的,那就是你写的sql语句有问题,
单步跟踪下 你的sql,如果没有发现问题 就复制该sql到查询分析器里 ,
让它执行一下 看看问题出在哪儿!!!
------解决方案--------------------

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 返回的是受影响的行数

试试这个!
}