“false”文本被插入到我的数据库中

时间:2011-10-24 13:59:52

标签: sql vb.net

我有一个带有许多DataGridViewComboBoxColumns的insert语句。当我尝试将这些DataGridViewComboBoxColumns的值插入到我的数据库中时,文本“False”将被插入到字段中。

为什么会这样?

Dim para As New SqlClient.SqlParameter
Dim dateValue As String = CStr(DateTimePicker1.Value)
Dim calendar = Microsoft.VisualBasic.Left(dateValue, 10)
para.ParameterName = "@company"
para.Value = company.Selected.ToString
para.ParameterName = "@division"
para.Value = division.Selected.ToString
para.ParameterName = "@date"
para.Value = DateTimePicker1.Value
para.ParameterName = "@supplier"
para.Value = supplier.Selected.ToString
para.ParameterName = "@material_group"
para.Value = materialGroup.Selected.ToString
para.ParameterName = "@cost"
para.Value = netprice.Selected.ToString
para.ParameterName = "@dsc"
para.Value = description.Selected.ToString
para.ParameterName = "@email"
para.Value = cmbEmail.SelectedValue
para.ParameterName = "@userID"
para.Value = id
para.ParameterName = "@Name"
para.Value = txtEmployee.Text
para.ParameterName = "@val"
para.Value = newName
para.ParameterName = "@quantity"
para.Value = quantity.Selected.ToString
para.ParameterName = "@wbs"
para.Value = wba.Selected.ToString
para.ParameterName = "@marketingCode"
para.Value = markCodes.Selected.ToString
para.ParameterName = "@hccNumber"
para.Value = hccNumber.Selected.ToString
para.ParameterName = "@currency"
para.Value = currency.Selected.ToString

插入详细信息:

Dim con As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
myCommand.Parameters.Add("@company", SqlDbType.NVarChar, 30).Value = company.Selected.ToString
myCommand.Parameters.Add("@division", SqlDbType.NVarChar, 30).Value = division.Selected.ToString
myCommand.Parameters.Add("@date", SqlDbType.NVarChar, 30).Value = DateTimePicker1.Value
myCommand.Parameters.Add("@supplier", SqlDbType.NVarChar, 30).Value = supplier.Selected.ToString
myCommand.Parameters.Add("@material_group", SqlDbType.NVarChar, 30).Value = materialGroup.Selected.ToString
myCommand.Parameters.Add("@cost", SqlDbType.NVarChar, 30).Value = netprice.Selected.ToString
myCommand.Parameters.Add("@dsc", SqlDbType.NVarChar, 30).Value = description.Selected.ToString
myCommand.Parameters.Add("@email", SqlDbType.NVarChar, 30).Value = cmbEmail.SelectedValue
myCommand.Parameters.Add("@userID", SqlDbType.NVarChar, 30).Value = id
myCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 30).Value = newName
myCommand.Parameters.Add("@quantity", SqlDbType.NVarChar, 40).Value = quantity.Selected.ToString
myCommand.Parameters.Add("@wbs", SqlDbType.NVarChar, 40).Value = wba.Selected.ToString
myCommand.Parameters.Add("@marketingCode", SqlDbType.NVarChar, 40).Value = markCodes.Selected.ToString
myCommand.Parameters.Add("@hccNumer", SqlDbType.NVarChar, 40).Value = hccNumber.ToString
myCommand.Parameters.Add("@currency", SqlDbType.NVarChar, 40).Value = currency.Selected.ToString

Dim myAdapter As New SqlClient.SqlDataAdapter
Dim sql As String = "Insert into details (company, division, date, supplier, material_group, cost, dsc, email, userID, fullName, marketingCode, wbs, qty, currency) VALUES (@company, @division, @date, @supplier, @material_group, @cost, @dsc, @email, @userID, @Name, @marketingCode, @wbs, @quantity, @currency)"
myCommand.Connection = con
myCommand.CommandText = sql
myAdapter.InsertCommand = myCommand
myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()

3 个答案:

答案 0 :(得分:4)

如果选择了某些内容,则存储。

para.Value = company.Selected.ToString 

Selected是一个布尔值!

答案 1 :(得分:2)

我不确定你在这里想要达到的目标,但我不认为这个脚本会如你所愿。您没有使用值构建参数列表,而是一遍又一遍地替换一个参数对象。

para.ParameterName = "@company"
para.Value = company.Selected.ToString
para.ParameterName = "@division"
para.Value = division.Selected.ToString

这将首先将para对象设置为@company,并将值设置为company.Selected.ToString,然后将其再次设置为@division并将值设置为division.Selected.ToString

您的数据类型全部为NVarChar,并且您明确将boolean值设置为字符串(.ToString())。

如果您希望存储布尔值,请将数据类型更改为bit。然后更改您的值,以便传递boolean值,而不是明确转换为booleanstring

myCommand.Parameters.Add("@currency", SqlDbType.bit).Value = currency.Selected

答案 2 :(得分:0)

如果选择了该项,则company.selected返回true,否则返回false ...

您可能想要使用company.ToString