尝试 运行 此代码,但我一直收到错误提示 "incorrect syntax near keyword 'where'" 请协助
trying to run this code but i keep getting an error saying "incorrect syntax near keyword 'where'" please assist
If _id <> String.Empty Then
cn.Open()
cm = New SqlCommand("update tblBill_Items set description = @description,qty = @qty,unitprice=@unitiprice,discount=@discount WHERE id=@id AND invoiceno=@invoiceno", cn)
With cm.Parameters
.AddWithValue("description", TextBox9.Text)
.AddWithValue("qty", CDbl(TextBox8.Text))
.AddWithValue("unitprice", CDbl(TextBox7.Text))
.AddWithValue("discount", CDbl(TextBox6.Text))
.AddWithValue("id", _id)
.AddWithValue("invoiceno", TextBox4.Text)
End With
cm.ExecuteNonQuery()
cn.Close()
Else
cn.Open()
cm = New SqlCommand("insert into tblBill_Items (invoiceno,description,qty,unitprice,discount) values(@invoiceno,@description,@qty,@unitprice,@discount)", cn)
With cm.Parameters
.AddWithValue("invoiceno", TextBox4.Text)
.AddWithValue("description", TextBox9.Text)
.AddWithValue("qty", CDbl(TextBox8.Text))
.AddWithValue("unitprice", CDbl(TextBox7.Text))
.AddWithValue("discount", CDbl(TextBox6.Text))
End With
cm.ExecuteNonQuery()
cn.Close()
End If
如果 _id 字段是一个字符串并且您的查询需要一个实际的字符串值,您应该将其用单引号引起来
在 OP 中,没有提供数据库列的数据类型,这本来很有用。我将讨论您的代码的一些潜在问题。我说“潜在”是因为不知道数据库列的数据类型并且没有表单的一些样本输入,就不可能确定某些问题可能出在哪里。
可能由于 AddWithValue
而出现问题。您可以考虑改用 Add
并显式指定数据类型,这将在下面的代码中使用。如果其中一个 TextBox 中的值为 null 或空,这将是一个问题。在这种情况下,需要将值设置为 DBNull.Value
.
_id <> String.Empty
和.AddWithValue("id", _id)
似乎都表明id
列是字符串数据类型,例如varchar
或nvarchar
。但是,在您的插入语句中,未指定 id
:
cm = New SqlCommand("insert into tblBill_Items (invoiceno,description,qty,unitprice,discount) values(@invoiceno,@description,@qty,@unitprice,@discount)", cn)
当一个值在插入时自动递增时,在插入过程中不需要指定列,因此数据类型可能是自动递增的小数,这使其成为主键的一个很好的候选者。然而,invoiceno
也应该是唯一的,这使得它成为主键的一个很好的候选者。就个人而言,我会删除 id
列并使用 invoiceno
作为主键。但是,在下面的代码中,我将 id
设为主键并设为 invoiceno
唯一,因为这是另一个可行的选择。
似乎没有任何检查以查看文本框中的任何值是否为 null 或空,尽管这可能是在 OP 中未包含的代码中完成的。似乎没有任何检查来确保在将值转换为数字值之前是数字值。如果 TextBox8.Text
包含 abc
会怎样?
CDbl(TextBox8.Text)
您可以考虑使用 Double.TryParse
或 Decimal.TryParse
。
在更新 SQL 语句中,WHERE id=@id AND invoiceno=@invoiceno
似乎没有必要同时包含 id
和 invoiceno
,因为它们可能都是唯一值。
下面显示了如何使用 VB.NET 在 SQL 服务器中创建 table。它还展示了如何在 table 中插入数据和更新数据。该代码已经使用 SQL Express 进行了测试。
- 数据库名称:订单
- Table 姓名: tblBill_items
注意:需要根据您的环境修改连接字符串。有关详细信息,请参阅 Connection Strings。 table 名称取自 OP。
在下面的代码中,id
列(在数据库 table 中)是十进制和自动增量。此外,invoiceno
是唯一的。
创建一个新的 Windows Forms App (.NET Framework)
项目
添加模块(名称:Helper.vb)
Helper.vb
Imports System.Data.SqlClient
Module Helper
'ToDo: modify the connection string for your environment
Private connectionStr As String = String.Format("Data Source='.\SQLExpress'; Initial Catalog='Order'; Integrated Security=True")
Public Sub CreateTblBillItems()
Dim sqlText As String = "CREATE TABLE tblBill_Items (id decimal NOT NULL IDENTITY(1,1)
CONSTRAINT PK_tblBill_Items_id PRIMARY KEY,
invoiceno nvarchar(50) NOT NULL
CONSTRAINT UQ_tblBill_Items_invoiceno UNIQUE,
description nvarchar(50),
qty decimal NOT NULL,
unitprice decimal(18,2) NOT NULL,
discount decimal(18,2))"
ExecuteNonQuery(sqlText)
End Sub
Private Sub ExecuteNonQuery(sqlText As String)
Try
Using cn As SqlConnection = New SqlConnection(connectionStr)
'open
cn.Open()
Using cmd As SqlCommand = New SqlCommand(sqlText, cn)
'execute
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As SqlException
Debug.WriteLine("Error (SqlException): " & ex.Message)
Throw ex
Catch ex As Exception
Debug.WriteLine("Error: " & ex.Message)
Throw ex
End Try
End Sub
Private Sub ExecuteNonQueryTblBillItems(sqlText As String, invoiceNo As String, description As String, qty As Double, unitPrice As Decimal, discount As Decimal, Optional id As Decimal = 0)
Try
Using cn As SqlConnection = New SqlConnection(connectionStr)
'open
cn.Open()
Using cmd As SqlCommand = New SqlCommand(sqlText, cn)
With cmd.Parameters
.Add("@id", SqlDbType.Decimal).Value = id
.Add("@invoiceno", SqlDbType.NVarChar).Value = If(String.IsNullOrEmpty(invoiceNo), DBNull.Value, invoiceNo)
.Add("@description", SqlDbType.NVarChar).Value = If(String.IsNullOrEmpty(description), DBNull.Value, description)
.Add("@qty", SqlDbType.Decimal).Value = qty
.Add("@unitprice", SqlDbType.Decimal).Value = unitPrice
.Add("@discount", SqlDbType.Decimal).Value = discount
End With
'ToDo: remove the following code that is for debugging
'For Each p As SqlParameter In cmd.Parameters
'Debug.WriteLine(p.ParameterName & ": " & p.Value.ToString())
'Next
'execute
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As SqlException
Debug.WriteLine("Error (SqlException): " & ex.Message)
Throw ex
Catch ex As Exception
Debug.WriteLine("Error: " & ex.Message)
Throw ex
End Try
End Sub
Public Sub InsertTblBillItems(invoiceNo As String, description As String, qty As Double, unitPrice As Decimal, discount As Decimal)
Dim sqlText As String = "INSERT INTO tblBill_Items(invoiceno, description, qty, unitprice, discount) VALUES(@invoiceno, @description, @qty, @unitprice, @discount)"
ExecuteNonQueryTblBillItems(sqlText, invoiceNo, description, qty, unitPrice, discount)
End Sub
Public Sub UpdateTblBillItems(invoiceNo As String, description As String, qty As Double, unitPrice As Decimal, discount As Decimal, id As Decimal)
Dim sqlText As String = "UPDATE tblBill_Items set invoiceno = @invoiceno, description = @description, qty = @qty, unitprice = @unitprice, discount = @discount WHERE invoiceno = @invoiceno"
ExecuteNonQueryTblBillItems(sqlText, invoiceNo, description, qty, unitPrice, discount, id)
End Sub
End Module
在 Form1 上,添加 Button、Label 和 TextBox 控件,如下所示:
双击每个按钮以添加“单击”事件处理程序。
Form1.vb
Public Class Form1
Private id As Decimal = 0
Private invoiceNo As String = String.Empty
Private description As String = String.Empty
Private qty As Double = 0
Private unitPrice As Decimal = 0
Private discount As Decimal = 0
Private Sub ButtonCreateTable_Click(sender As Object, e As EventArgs) Handles ButtonCreateTable.Click
Helper.CreateTblBillItems()
Debug.WriteLine("Info: Table 'tblBill_Items' created")
End Sub
Private Function IsInputAvailable() As Boolean
If String.IsNullOrEmpty(TextBoxInvoiceNo.Text) Then
MessageBox.Show("InvoiceNo not specified", "Error - InvoiceNo", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
If String.IsNullOrEmpty(TextBoxDescription.Text) Then
MessageBox.Show("Description not specified", "Error - Description", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
If String.IsNullOrEmpty(TextBoxQty.Text) Then
MessageBox.Show("Qty not specified", "Error - Qty", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
If String.IsNullOrEmpty(TextBoxUnitPrice.Text) Then
MessageBox.Show("UnitPrice not specified", "Error - UnitPrice", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
Return True
End Function
Private Sub GetData()
'get/set values
'try to convert
Decimal.TryParse(TextBoxId.Text, id)
invoiceNo = TextBoxInvoiceNo.Text.Trim()
description = TextBoxDescription.Text.Trim()
qty = 0
unitPrice = 0
discount = 0
'convert to Double
Double.TryParse(TextBoxQty.Text, qty)
'convert to Decimal
Decimal.TryParse(TextBoxUnitPrice.Text, unitPrice)
If Not String.IsNullOrEmpty(TextBoxDiscount.Text) Then
'convert to Decimal
Decimal.TryParse(TextBoxDiscount.Text, discount)
End If
End Sub
Private Sub BtnInsert_Click(sender As Object, e As EventArgs) Handles BtnInsert.Click
If IsInputAvailable() Then
GetData()
Helper.InsertTblBillItems(invoiceNo, description, qty, unitPrice, discount)
Debug.WriteLine("Info: Data inserted into 'tblBill_Items'")
End If
End Sub
Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click
If IsInputAvailable() Then
GetData()
Helper.UpdateTblBillItems(invoiceNo, description, qty, unitPrice, discount, id)
Debug.WriteLine("Info: Table 'tblBill_Items' updated")
End If
End Sub
End Class
资源:
If _id <> String.Empty Then
cn.Open()
cm = New SqlCommand("update tblBill_Items set description = @description,qty = @qty,unitprice=@unitiprice,discount=@discount WHERE id=@id AND invoiceno=@invoiceno", cn)
With cm.Parameters
.AddWithValue("description", TextBox9.Text)
.AddWithValue("qty", CDbl(TextBox8.Text))
.AddWithValue("unitprice", CDbl(TextBox7.Text))
.AddWithValue("discount", CDbl(TextBox6.Text))
.AddWithValue("id", _id)
.AddWithValue("invoiceno", TextBox4.Text)
End With
cm.ExecuteNonQuery()
cn.Close()
Else
cn.Open()
cm = New SqlCommand("insert into tblBill_Items (invoiceno,description,qty,unitprice,discount) values(@invoiceno,@description,@qty,@unitprice,@discount)", cn)
With cm.Parameters
.AddWithValue("invoiceno", TextBox4.Text)
.AddWithValue("description", TextBox9.Text)
.AddWithValue("qty", CDbl(TextBox8.Text))
.AddWithValue("unitprice", CDbl(TextBox7.Text))
.AddWithValue("discount", CDbl(TextBox6.Text))
End With
cm.ExecuteNonQuery()
cn.Close()
End If
如果 _id 字段是一个字符串并且您的查询需要一个实际的字符串值,您应该将其用单引号引起来
在 OP 中,没有提供数据库列的数据类型,这本来很有用。我将讨论您的代码的一些潜在问题。我说“潜在”是因为不知道数据库列的数据类型并且没有表单的一些样本输入,就不可能确定某些问题可能出在哪里。
可能由于 AddWithValue
而出现问题。您可以考虑改用 Add
并显式指定数据类型,这将在下面的代码中使用。如果其中一个 TextBox 中的值为 null 或空,这将是一个问题。在这种情况下,需要将值设置为 DBNull.Value
.
_id <> String.Empty
和.AddWithValue("id", _id)
似乎都表明id
列是字符串数据类型,例如varchar
或nvarchar
。但是,在您的插入语句中,未指定 id
:
cm = New SqlCommand("insert into tblBill_Items (invoiceno,description,qty,unitprice,discount) values(@invoiceno,@description,@qty,@unitprice,@discount)", cn)
当一个值在插入时自动递增时,在插入过程中不需要指定列,因此数据类型可能是自动递增的小数,这使其成为主键的一个很好的候选者。然而,invoiceno
也应该是唯一的,这使得它成为主键的一个很好的候选者。就个人而言,我会删除 id
列并使用 invoiceno
作为主键。但是,在下面的代码中,我将 id
设为主键并设为 invoiceno
唯一,因为这是另一个可行的选择。
似乎没有任何检查以查看文本框中的任何值是否为 null 或空,尽管这可能是在 OP 中未包含的代码中完成的。似乎没有任何检查来确保在将值转换为数字值之前是数字值。如果 TextBox8.Text
包含 abc
会怎样?
CDbl(TextBox8.Text)
您可以考虑使用 Double.TryParse
或 Decimal.TryParse
。
在更新 SQL 语句中,WHERE id=@id AND invoiceno=@invoiceno
似乎没有必要同时包含 id
和 invoiceno
,因为它们可能都是唯一值。
下面显示了如何使用 VB.NET 在 SQL 服务器中创建 table。它还展示了如何在 table 中插入数据和更新数据。该代码已经使用 SQL Express 进行了测试。
- 数据库名称:订单
- Table 姓名: tblBill_items
注意:需要根据您的环境修改连接字符串。有关详细信息,请参阅 Connection Strings。 table 名称取自 OP。
在下面的代码中,id
列(在数据库 table 中)是十进制和自动增量。此外,invoiceno
是唯一的。
创建一个新的 Windows Forms App (.NET Framework)
项目
添加模块(名称:Helper.vb)
Helper.vb
Imports System.Data.SqlClient
Module Helper
'ToDo: modify the connection string for your environment
Private connectionStr As String = String.Format("Data Source='.\SQLExpress'; Initial Catalog='Order'; Integrated Security=True")
Public Sub CreateTblBillItems()
Dim sqlText As String = "CREATE TABLE tblBill_Items (id decimal NOT NULL IDENTITY(1,1)
CONSTRAINT PK_tblBill_Items_id PRIMARY KEY,
invoiceno nvarchar(50) NOT NULL
CONSTRAINT UQ_tblBill_Items_invoiceno UNIQUE,
description nvarchar(50),
qty decimal NOT NULL,
unitprice decimal(18,2) NOT NULL,
discount decimal(18,2))"
ExecuteNonQuery(sqlText)
End Sub
Private Sub ExecuteNonQuery(sqlText As String)
Try
Using cn As SqlConnection = New SqlConnection(connectionStr)
'open
cn.Open()
Using cmd As SqlCommand = New SqlCommand(sqlText, cn)
'execute
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As SqlException
Debug.WriteLine("Error (SqlException): " & ex.Message)
Throw ex
Catch ex As Exception
Debug.WriteLine("Error: " & ex.Message)
Throw ex
End Try
End Sub
Private Sub ExecuteNonQueryTblBillItems(sqlText As String, invoiceNo As String, description As String, qty As Double, unitPrice As Decimal, discount As Decimal, Optional id As Decimal = 0)
Try
Using cn As SqlConnection = New SqlConnection(connectionStr)
'open
cn.Open()
Using cmd As SqlCommand = New SqlCommand(sqlText, cn)
With cmd.Parameters
.Add("@id", SqlDbType.Decimal).Value = id
.Add("@invoiceno", SqlDbType.NVarChar).Value = If(String.IsNullOrEmpty(invoiceNo), DBNull.Value, invoiceNo)
.Add("@description", SqlDbType.NVarChar).Value = If(String.IsNullOrEmpty(description), DBNull.Value, description)
.Add("@qty", SqlDbType.Decimal).Value = qty
.Add("@unitprice", SqlDbType.Decimal).Value = unitPrice
.Add("@discount", SqlDbType.Decimal).Value = discount
End With
'ToDo: remove the following code that is for debugging
'For Each p As SqlParameter In cmd.Parameters
'Debug.WriteLine(p.ParameterName & ": " & p.Value.ToString())
'Next
'execute
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As SqlException
Debug.WriteLine("Error (SqlException): " & ex.Message)
Throw ex
Catch ex As Exception
Debug.WriteLine("Error: " & ex.Message)
Throw ex
End Try
End Sub
Public Sub InsertTblBillItems(invoiceNo As String, description As String, qty As Double, unitPrice As Decimal, discount As Decimal)
Dim sqlText As String = "INSERT INTO tblBill_Items(invoiceno, description, qty, unitprice, discount) VALUES(@invoiceno, @description, @qty, @unitprice, @discount)"
ExecuteNonQueryTblBillItems(sqlText, invoiceNo, description, qty, unitPrice, discount)
End Sub
Public Sub UpdateTblBillItems(invoiceNo As String, description As String, qty As Double, unitPrice As Decimal, discount As Decimal, id As Decimal)
Dim sqlText As String = "UPDATE tblBill_Items set invoiceno = @invoiceno, description = @description, qty = @qty, unitprice = @unitprice, discount = @discount WHERE invoiceno = @invoiceno"
ExecuteNonQueryTblBillItems(sqlText, invoiceNo, description, qty, unitPrice, discount, id)
End Sub
End Module
在 Form1 上,添加 Button、Label 和 TextBox 控件,如下所示:
双击每个按钮以添加“单击”事件处理程序。
Form1.vb
Public Class Form1
Private id As Decimal = 0
Private invoiceNo As String = String.Empty
Private description As String = String.Empty
Private qty As Double = 0
Private unitPrice As Decimal = 0
Private discount As Decimal = 0
Private Sub ButtonCreateTable_Click(sender As Object, e As EventArgs) Handles ButtonCreateTable.Click
Helper.CreateTblBillItems()
Debug.WriteLine("Info: Table 'tblBill_Items' created")
End Sub
Private Function IsInputAvailable() As Boolean
If String.IsNullOrEmpty(TextBoxInvoiceNo.Text) Then
MessageBox.Show("InvoiceNo not specified", "Error - InvoiceNo", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
If String.IsNullOrEmpty(TextBoxDescription.Text) Then
MessageBox.Show("Description not specified", "Error - Description", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
If String.IsNullOrEmpty(TextBoxQty.Text) Then
MessageBox.Show("Qty not specified", "Error - Qty", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
If String.IsNullOrEmpty(TextBoxUnitPrice.Text) Then
MessageBox.Show("UnitPrice not specified", "Error - UnitPrice", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
Return True
End Function
Private Sub GetData()
'get/set values
'try to convert
Decimal.TryParse(TextBoxId.Text, id)
invoiceNo = TextBoxInvoiceNo.Text.Trim()
description = TextBoxDescription.Text.Trim()
qty = 0
unitPrice = 0
discount = 0
'convert to Double
Double.TryParse(TextBoxQty.Text, qty)
'convert to Decimal
Decimal.TryParse(TextBoxUnitPrice.Text, unitPrice)
If Not String.IsNullOrEmpty(TextBoxDiscount.Text) Then
'convert to Decimal
Decimal.TryParse(TextBoxDiscount.Text, discount)
End If
End Sub
Private Sub BtnInsert_Click(sender As Object, e As EventArgs) Handles BtnInsert.Click
If IsInputAvailable() Then
GetData()
Helper.InsertTblBillItems(invoiceNo, description, qty, unitPrice, discount)
Debug.WriteLine("Info: Data inserted into 'tblBill_Items'")
End If
End Sub
Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click
If IsInputAvailable() Then
GetData()
Helper.UpdateTblBillItems(invoiceNo, description, qty, unitPrice, discount, id)
Debug.WriteLine("Info: Table 'tblBill_Items' updated")
End If
End Sub
End Class
资源: