查看: 1562|回复: 7
|
请问:VB.NET Add Record 问题
[复制链接]
|
|
- Private Sub btnAddOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddOrder.Click
-
- Dim myConnectionString As String = "workstation id=YZME;packet size=4096;user id=sa;pwd=password;data source=(local);persist security info=False;initial catalog=Tracker"
-
- Dim myConnection As New System.Data.SqlClient.SqlConnection(myConnectionString)
- Dim CustID As String = txtCustID.Text
- Dim OrderDate As String = txtOrderDate.Text
- Dim RequireDate As String = txtRequiredDate.Text
- Dim ShippedDate As String = txtShippedDate.Text
- Dim ShipAddress As String = txtShipAddress.Text
- Dim PostCode As String = txtPostCode.Text
- Dim Country As String = txtCountry.Text
- Dim myInsertQuery As String = "INSERT INTO [Order](CustomerID,Order_Date,Required_Date,Shipped_Date,ShippedAddress,PostCode,Country)" & _
- "Values(CustID,OrderDate,RequireDate,ShippedDate,ShipAddress,PostCode,Country)"
- Dim myCommand As New System.Data.SqlClient.SqlCommand(myInsertQuery)
- myCommand.Connection = myConnection
- myConnection.Open()
- myCommand.ExecuteNonQuery()
- myCommand.Connection.Close()
- End Sub
复制代码
有办法吗?
cannot add record into database
how to solve it |
|
|
|
|
|
|
|
发表于 15-10-2004 10:18 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 15-10-2004 11:40 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 16-10-2004 10:40 AM
|
显示全部楼层
Dim myInsertQuery As String = "INSERT INTO [Order](CustomerID,Order_Date,Required_Date,Shipped_Date,ShippedAddress,PostCode,Country)" & _
"Values(CustID,OrderDate,RequireDate,ShippedDate,ShipAddress,PostCode,Country)"
會不會是空格(SPACE)的問題? |
|
|
|
|
|
|
|
发表于 16-10-2004 11:42 AM
|
显示全部楼层
你的FIELD OR VALUE 有放错位置吗?
它出现什么ERROR MSG? |
|
|
|
|
|
|
|
发表于 16-10-2004 01:02 PM
|
显示全部楼层
问题应该是这个
"Values(CustID,OrderDate,RequireDate,ShippedDate,ShipAddress,PostCode,Country)"
sql server只会把custID, orderdate等当成是value,而不知道那是vb.net里头的variable...
至少要把它换成类似这样:
"values('" & custID & "')"
不过最好还是用它的parameter object...
然后把statement变成
"Insert table(custID, orderDate) values(@custID, @orderDate)"
参考这里: http://msdn.microsoft.com/librar ... parameterstopic.asp
[ Last edited by astral on 16-10-2004 at 01:03 PM ] |
|
|
|
|
|
|
|
![](static/image/common/ico_lz.png)
楼主 |
发表于 16-10-2004 02:23 PM
|
显示全部楼层
when i debug..the error say
this line got problem...i think it is not the reason
- myCommand.ExecuteNonQuery()
复制代码
when i use , it should be no problem...
- 'Dim myInsertQuery As String = "INSERT INTO [Order](CustomerID,Order_Date,Required_Date,Shipped_Date,ShippedAddress,PostCode,Country)" & _
- ' "Values('1001','5/5/2004','5/5/2004','5/5/2004','330 Seremban','70450','Malaysia')"
复制代码
but when i assign each one with particular
- Dim CustID As String = txtCustID.Text
- Dim OrderDate As String = txtOrderDate.Text
- Dim RequireDate As String = txtRequiredDate.Text
- Dim ShippedDate As String = txtShippedDate.Text
- Dim ShipAddress As String = txtShipAddress.Text
- Dim PostCode As String = txtPostCode.Text
- Dim Country As String = txtCountry.Text
复制代码
maybe astral was right...but how to use parameter object...
can u show me... |
|
|
|
|
|
|
|
发表于 16-10-2004 04:52 PM
|
显示全部楼层
try this
OleDbConnection1.Open()
OleDbCommand1.CommandText = _
"INSERT INTO xxx(xxx, xxx, xxx)" & _
"VALUES('" & TextBox1.Text & "', '" & TextBox2.Text & "','" & TextBox3.Text & "')"
OleDbCommand1.ExecuteNonQuery()
OleDbConnection1.Close()
if have this problem OleDbCommand1.ExecuteNonQuery()<---- maybe is your database FIELD ERROR |
|
|
|
|
|
|
| |
本周最热论坛帖子
|