我连接的数据库是.dbf文件
string insertSQL = "insert into [sg0510wyys.dbf] (mondate,stname,w_temp) " +"VALUES (?, ?, ?)";OdbcCommand insertCMD = new OdbcCommand(insertSQL, cnn);
da.InsertCommand = insertCMD;
da.InsertCommand.Parameters.Add("@mondate", OdbcType.DateTime);
da.InsertCommand.Parameters["@mondate"].SourceColumn="mondate";
da.InsertCommand.Parameters.Add("@stname", OdbcType.VarChar);
da.InsertCommand.Parameters["@stname"].SourceColumn="stname";
da.InsertCommand.Parameters.Add("@w_temp", OdbcType.Decimal);
da.InsertCommand.Parameters["@w_temp"].SourceColumn="w_temp";
da.Fill(ds,"table1");
this.dataGrid1.DataSource=ds.Tables["table1"].DefaultView;
====================================================================
DataAdapter的insetcommand,updatecommand,selectcommand,deletecommand都已写好,
数据库中的w_temp字段是数字类型decimal
问题是当我在dataGrid中新添数据后,用update不能将数据写入数据库中,因为@w_temp的值通过sourcecolumn取得的值是字符串(虽然定义为decimal,但从DataTable中读出就为字符串了),而在前面的insert语句中VALUES (?, ?, ?)的第三个参数?必须为decimal才能插入到数据库中,
请问如何将如何办,转换类型????????在线等待!!!!该字段绑定了数据源列,并没有通过
da.InsertCommand.Parameters["@w_temp"].value来赋值,
?这个参数获得是字符串,
能不能通过
string insertSQL = "insert into [sg0510wyys.dbf] (mondate,stname,w_temp) " +"VALUES (?, ?, ?)";
convert(decimal,?)

解决方案 »

  1.   

    是foxpro的.dbf...
    数据表中的字段w_temp是decimal的..
    我让它绑定了DataTable中的数据源列,就不用自己动手给它赋值..它的值在DataGrid中自动获得
    da.InsertCommand.Parameters.Add("@w_temp", OdbcType.Decimal);
    da.InsertCommand.Parameters["@w_temp"].SourceColumn="w_temp";但得到的da.InsertCommand.Parameters["@w_temp"].value为字符串,
    当它执行
    string insertSQL = "insert into [sg0510wyys.dbf] (mondate,stname,w_temp) " +"VALUES (?, ?, ?)";
    时出错,,第三个参数(?)必须为decimal才得插入成功...
      

  2.   

    那你跟踪下w_temp 看看它是否符合Decimal的定义
    另外 你这个程序构造的也挺麻烦的 还不如直接构造好SQL语句 而不要用参数方式了
    比如这样:
    string insertSQL = "insert into [sg0510wyys.dbf] (mondate,stname,w_temp) " +"VALUES ("+mondate+","+stname+","+w_temp+")";
      

  3.   

    是这样的.我DataAdapter生成了insertcommand,updatecommand,等等,,,里面当然是用存储过程啦.....
    当在DataGrid中添加记录,修改记录后(不只一项记录)用Update就会自动调用insertcommand,updatecommand等语句,,,?,?的值就通过它绑定的数据源列获得....所以楼上所说的
    string insertSQL = "insert into [sg0510wyys.dbf] (mondate,stname,w_temp) " +"VALUES ("+mondate+","+stname+","+w_temp+")";
    ---------------mondate stname w_temp我并知道它的值是什么,也不能通过DataRow获得啊
      

  4.   

    private void DataGrid3_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType!=ListItemType.Header && e.Item.ItemType!=ListItemType.Footer && e.Item.ItemType !=ListItemType.EditItem)
    {
    string str=DataGrid3.DataKeys[e.Item.ItemIndex].ToString();
    CheckBox ch=(CheckBox)e.Item.FindControl("chk1");
    ch.Attributes.Add("onclick","javascript:alert('你选中了我"+e.Item.ItemIndex.ToString()+"');");
    }
    }
    string str=DataGrid3.DataKeys[e.Item.ItemIndex].ToString(); 
    if(e.Item.ItemType!=ListItemType.Header && e.Item.ItemType!=ListItemType.Footer && e.Item.ItemType !=ListItemType.EditItem)  
    CheckBox ch=(CheckBox)e.Item.FindControl("chk1");
    ch.Attributes.Add("onclick","javascript:alert('你选中了我"+e.Item.ItemIndex.ToString()+"');");
    }
      

  5.   

    我没有用到updatecommand,,不需要设置主键,,,
    我只用到Insertcommand,,,提示错误,,,"类型错误"....
      

  6.   

    另外,mondate,stname这两个值可以插入,,但w_temp就是插入不进,原因上面已说明
      

  7.   

    说到底,就是GetValue返回的Object强制转换成特定类型所需的处理没有实现到,,
    InsertCommand.Parameter["@w_temp'].value=Convert.Todecimal(ds.tables[0].row[行][w_temp])
    我不知道是哪行(也可能是多行),所以上面这句不能用....有什么办法将它的值object转换成decimal.............