sql 来自 vb 的更新仅部分有效
sql update from vb only works partially
我正在尝试通过 VB(2012) 在 mssql2012 中更新 table。
如果我使用第一个代码(缺少 4 个字段),它会起作用。
使用额外的 4 个字段 (tire_id2, qty2, cost2, retail2
) 我得到错误:
failed to convert parameter value from a string to 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()
和"bad"代码:
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()
有什么想法吗?
这是我对 sql table:
的 "CREATE" 声明
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)
在这一行。
mycommand.Parameters.Add("@date", SqlDbType.Date).Value = txtQuoteDate.Text
请试试这个。
mycommand.Parameters.Add("@date", SqlDbType.Date).Value = cDate(txtQuoteDate.Text)
我正在尝试通过 VB(2012) 在 mssql2012 中更新 table。
如果我使用第一个代码(缺少 4 个字段),它会起作用。
使用额外的 4 个字段 (tire_id2, qty2, cost2, retail2
) 我得到错误:
failed to convert parameter value from a string to 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()
和"bad"代码:
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()
有什么想法吗? 这是我对 sql table:
的 "CREATE" 声明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)
在这一行。
mycommand.Parameters.Add("@date", SqlDbType.Date).Value = txtQuoteDate.Text
请试试这个。
mycommand.Parameters.Add("@date", SqlDbType.Date).Value = cDate(txtQuoteDate.Text)