来自vb的sql更新只能部分工作

时间:2015-03-20 02:50:29

标签: sql-server visual-studio-2012

我正在尝试通过VB(2012)更新mssql2012中的表。

如果我使用第一个代码(缺少4个字段),它就可以工作。

使用额外的4个字段(tire_id2, qty2, cost2, retail2),我收到错误:

  

无法将参数值从字符串转换为DateTime。

好的代码:

 mycommand.Connection = m_cn
    mycommand.CommandText = "INSERT INTO sales (date, cust_id, tire_id, qty, cost_ea, retail_ea, plate,mileage,service,service2,serv_price,serv2_price,total,notes,custphone) VALUES (@date, @cust_id, @tire_id,@qty, @cost, @retail,@plate,@mileage,@service,@service2,@serv_price,@serv2_price,@total,@notes,@custphone)"

    mycommand.Parameters.Add("@date", SqlDbType.Date).Value = txtQuoteDate.Text
    mycommand.Parameters.Add("@cust_id", SqlDbType.NVarChar).Value = cust_datatable.Rows(0).Item("custID")
    mycommand.Parameters.Add("@tire_id", SqlDbType.NVarChar).Value = (m_intRowPosition + 1)
    mycommand.Parameters.Add("@qty", SqlDbType.VarChar).Value = txtQuantity.Text
    mycommand.Parameters.Add("@cost", SqlDbType.VarChar).Value = m_DataTable.Rows(m_intRowPosition)("cost").ToString
    mycommand.Parameters.Add("@retail", SqlDbType.NVarChar).Value = txtEachPrice.Text
    mycommand.Parameters.Add("@plate", SqlDbType.VarChar).Value = txtplate.Text
    mycommand.Parameters.Add("@mileage", SqlDbType.NVarChar).Value = txtmileage.Text
    mycommand.Parameters.Add("@service", SqlDbType.NVarChar).Value = txtService1.Text
    mycommand.Parameters.Add("@serv_price", SqlDbType.VarChar).Value = txtAlignPrice.Text
    mycommand.Parameters.Add("@total", SqlDbType.NVarChar).Value = txtTotal.Text
    mycommand.Parameters.Add("@notes", SqlDbType.NVarChar).Value = txtNotes.Text
    mycommand.Parameters.Add("@custphone", SqlDbType.NVarChar).Value = txtCustPhone.Text
    mycommand.Parameters.Add("@service2", SqlDbType.NVarChar).Value = txtService2.Text
    mycommand.Parameters.Add("@serv2_price", SqlDbType.VarChar).Value = txtService2Price.Text
    mycommand.ExecuteNonQuery()

和"坏"代码:

 mycommand.Connection = m_cn
        mycommand.CommandText = "INSERT INTO sales (date, cust_id, tire_id,tire_id2, qty, qty2, cost_ea, cost_ea2, retail_ea, retail_e2, plate,mileage,service,service2,serv_price,serv2_price,total,notes,custphone) VALUES (@date, @cust_id, @tire_id, @tire_id2,@qty, @qty2, @cost, @cost2, @retail,@retail2,@plate,@mileage,@service,@service2,@serv_price,@serv2_price,@total,@notes,@custphone)"

        mycommand.Parameters.Add("@date", SqlDbType.Date).Value = txtQuoteDate.Text
        mycommand.Parameters.Add("@cust_id", SqlDbType.NVarChar).Value = cust_datatable.Rows(0).Item("custID")
        mycommand.Parameters.Add("@tire_id", SqlDbType.NVarChar).Value = (m_intRowPosition + 1)
        mycommand.Parameters.Add("@tire_id2", SqlDbType.NVarChar).Value = (m_introwposition2 + 1)
        mycommand.Parameters.Add("@qty", SqlDbType.VarChar).Value = txtQuantity.Text
        mycommand.Parameters.Add("@qty2", SqlDbType.VarChar).Value = txtQuantity2.Text
        mycommand.Parameters.Add("@cost", SqlDbType.VarChar).Value = m_DataTable.Rows(m_intRowPosition)("cost").ToString
        mycommand.Parameters.Add("@cost2", SqlDbType.VarChar).Value = m_datatable2.Rows(m_introwposition2)("cost").ToString
        mycommand.Parameters.Add("@retail", SqlDbType.NVarChar).Value = txtEachPrice.Text
        mycommand.Parameters.Add("@retail2", SqlDbType.NVarChar).Value = txtEachPrice2.Text
        mycommand.Parameters.Add("@plate", SqlDbType.VarChar).Value = txtplate.Text
        mycommand.Parameters.Add("@mileage", SqlDbType.NVarChar).Value = txtmileage.Text
        mycommand.Parameters.Add("@service", SqlDbType.NVarChar).Value = txtService1.Text
        mycommand.Parameters.Add("@serv_price", SqlDbType.VarChar).Value = txtAlignPrice.Text
        mycommand.Parameters.Add("@total", SqlDbType.NVarChar).Value = txtTotal.Text
        mycommand.Parameters.Add("@notes", SqlDbType.NVarChar).Value = txtNotes.Text
        mycommand.Parameters.Add("@custphone", SqlDbType.NVarChar).Value = txtCustPhone.Text
        mycommand.Parameters.Add("@service2", SqlDbType.NVarChar).Value = txtService2.Text
        mycommand.Parameters.Add("@serv2_price", SqlDbType.VarChar).Value = txtService2Price.Text
        mycommand.ExecuteNonQuery()

有什么想法? 这是我的" CREATE" sql表的语句:

CREATE TABLE [dbo].[sales](
[date] [date] NULL,
[cust_id] [int] NULL,
[tire_id] [int] NULL,
[qty] [int] NULL,
[cost_ea] [decimal](5, 2) NULL,
[retail_ea] [decimal](5, 2) NULL,
[plate] [nchar](10) NULL,
[mileage] [nchar](10) NULL,
[service] [nchar](30) NULL,
[serv_price] [nchar](10) NULL,
[total] [nchar](10) NULL,
[notes] [nvarchar](150) NULL,
[custphone] [nchar](10) NULL,
[service2] [nchar](30) NULL,
[serv2_price] [nchar](10) NULL,
[inv_no] [int] IDENTITY(1,1) NOT NULL,
[tire_id2] [int] NULL,
[qty2] [int] NULL,
[cost_ea2] [decimal](5, 2) NULL,
[retail_ea2] [decimal](5, 2) NULL,
[tire_id3] [int] NULL,
[qty3] [int] NULL,
[cost_ea3] [decimal](5, 2) NULL,
[retail_ea3] [decimal](5, 2) NULL,
CONSTRAINT [PK_sales] PRIMARY KEY CLUSTERED)

1 个答案:

答案 0 :(得分:0)

在这一行。

mycommand.Parameters.Add("@date", SqlDbType.Date).Value = txtQuoteDate.Text

请试一试。

mycommand.Parameters.Add("@date", SqlDbType.Date).Value = cDate(txtQuoteDate.Text)