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

数据插入问题,我是个菜鸟

(1) int id = int.Parse(this.TextBox2.Text);
  string name = this.TextBox3.Text;
  int mid = int.Parse(this.TextBox4.Text);
  double lid = double .Parse(this.TextBox5.Text);
  SqlCommand com = con.CreateCommand();
  string sql ="insert into DEPARTMENTS values(@id,@name,@mid,@lid)";
com.Parameters.Add(new SqlParameter("@id", id));
com.Parameters.Add(new SqlParameter("@name", name));
com.Parameters.Add(new SqlParameter("@mid", mid));
com.Parameters.Add(new SqlParameter("@lid", lid));  
   
  com.CommandText = sql;
  com.CommandType = CommandType.Text;  
  com.ExecuteNonQuery();


string sql = string.Format("insert into DEPARTMENTS values({0},{1},{2},{3})",id,name ,mid,lid);

红色的部分用下面的连接语句会报一个异常,怎么回事?求高手!

------解决方案--------------------
学一下断点调试,看各变量的值怎样...
string sql ="insert into DEPARTMENTS values(@id,@name,@mid,@lid)";
string sql = string.Format("insert into DEPARTMENTS values({0},{1},{2},{3})",id,name ,mid,lid);
是不是定义了两次sql?
------解决方案--------------------
语句没有问题,可以调试程序看看