Varchar不允许使用Varbin

时间:2012-04-06 15:30:58

标签: c# sql-server-2008

在将值插入数据库时​​获取错误(SQL Server 2008)

  

不允许从数据类型varchar到varbinary的隐式转换。使用CONVERT函数运行此查询。

代码:

da.InsertCommand = new SqlCommand("insert into tblProductMaster ([Name],[Type],[Height],[Width],[Length],[Price]) values( @Name , @Type , @Height , @Width , @Length , @Price )", con);

da.InsertCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
da.InsertCommand.Parameters.Add("@Type", SqlDbType.VarChar, 10).Value = Convert.ToString(ddlType.SelectedItem);
da.InsertCommand.Parameters.Add("@Height", SqlDbType.Float).Value = float.Parse(txtHeight.Text);
da.InsertCommand.Parameters.Add("@Width", SqlDbType.Float).Value = float.Parse(txtWidth.Text);
da.InsertCommand.Parameters.Add("@Length", SqlDbType.Float).Value = float.Parse(txtLength.Text);
da.InsertCommand.Parameters.Add("@Price", SqlDbType.SmallMoney).Value = int.Parse(txtPrice.Text);

da.InsertCommand.ExecuteNonQuery();

1 个答案:

答案 0 :(得分:0)

我认为表中的Name或Type列的类型为varbinary,并且您尝试将它们设置为varchar