我的表中有一些字段,格式化为四位小数(即0.0125)。
我在Visual Basic中使用NumericUpDown对象来收集我想要传递给我的数据库的数字。
不幸的是,当我运行我的代码时,我收到以下错误:
从字符串“(CallType,ChargeCode,Destinatio”到“Double”类型的转换无效。
以下是我用来将值推送到数据库的代码的副本。
Using cmdc = New SqlCommand("INSERT INTO " & TextBox2.Text
& " (CallType,
ChargeCode,
Destination,
TariffUsed,
(case when chargecode in ('" + Combobox2.text + "') then '" + NumericUpDown1.value + "'
else Peak*2 end) as Peak,
(case when chargecode in ('" + Combobox2.text + "') then '" + NumericUpDown2.value + "'
else OffPeak*2 end) as OffPeak,
(case when chargecode in ('" + Combobox2.text + "') then '" + NumericUpDown3.value + "'
else Weekend*2 end) as Weekend,
(case when chargecode in ('" + Combobox2.text + "') then '" + NumericUpDown4.value + "'
else Setup*2 end) as Setup,
(case when chargecode in ('" + Combobox2.text + "') then '" + NumericUpDown5.value + "'
else MinimumCharge*2 end) as MinimumCharge,
ChargeCap,
InitialUnits,
InitialCharge,
InitialPeak,
InitialOffPeak,
InitialWeekend,
BillingUnit,
MinimumUnits,
RateType
FROM " & ComboBox1.Text & "", con)
con.Open()
cmdc.ExecuteNonQuery()
con.Close()
End Using
End Using
由于SQL中不存在DOUBLE,因此我将目标字段配置为FLOAT。
是否可以将NumericUpDown中的值传递到我的数据库中,如果是这样,那么正确的格式是什么?
答案 0 :(得分:0)
非常感谢@Paqogomez:
ToString()解决了这个问题。
以下是工作代码:
Using cmdc = New SqlCommand("Insert into " & TextBox2.Text & " Select CallType,ChargeCode,Destination,TariffUsed,(case when chargecode in ('" + ComboBox2.Text + "') then " + NumericUpDown1.Value.ToString() + " else Peak*2 end) as Peak,(case when chargecode in ('" + ComboBox2.Text + "') then " + NumericUpDown2.Value.ToString() + " else OffPeak*2 end) as OffPeak,(case when chargecode in ('" + ComboBox2.Text + "') then " + NumericUpDown3.Value.ToString() + " else Weekend*2 end) as Weekend,(case when chargecode in ('" + ComboBox2.Text + "') then " + NumericUpDown4.Value.ToString() + " else Setup*2 end) as Setup,(case when chargecode in ('" + ComboBox2.Text + "') then " + NumericUpDown5.Value.ToString() + " else MinimumCharge*2 end) as MinimumCharge,ChargeCap,InitialUnits,InitialCharge,InitialPeak,InitialOffPeak,InitialWeekend,BillingUnit,MinimumUnits,RateType from Daisy_March2014", con)
con.Open()
cmdc.Parameters.AddWithValue("@tarrif2", TextBox2.Text)
cmdc.Parameters.AddWithValue("@DateTime", DateTime.Now())
cmdc.ExecuteNonQuery()
con.Close()
End Using
End Using
*使用风险自负,应该真正参数化!