我差不多完成了我的代码,但之前从未将数据插入数据库。到目前为止,我的SQL语句工作正常,但最终执行的代码我不知道该怎么做。
新记录没有出现在我的数据库中,所以我想我最后遗漏了几行,使新记录执行。
Protected Sub Insert_Click(sender As Object, e As EventArgs) Handles Insert.Click
Dim v_Date = DateText.Text
Dim v_Username = UserText.Text
Dim v_Phone = PhoneText.Text
Dim v_Email = EmailText.Text
Dim v_Category = CategoryList.Text
Dim v_Short = ShortText.Text
Dim v_Long = longText.Value
Dim conn As OleDbConnection = New OleDbConnection("Provider=""*********"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)
Dim ClassifiedStr As OleDbCommand = New OleDbCommand("INSERT INTO TABLENAME (Date, Username, Phonenbr, Email, Category, Description, Fulldescription) values('" & v_Date & "',lower('" & v_Username & "'),'" & v_Phone & "','" & v_Email & "','" & v_Category & "','" & v_Short & "','" & v_Long & "'", conn)
'ClassifiedStr.CommandType = CommandType.StoredProcedure
Dim OracleDataAdapterAds As OleDbDataAdapter = New OleDbDataAdapter
OracleDataAdapterAds.SelectCommand = ClassifiedStr
Dim DsAds As DataSet = New DataSet
DsAds.Clear()
'OracleDataAdapterAds.Fill(DsAds, "TABLENAME")
conn.Open()
'ClassifiedStr.ExecuteNonQuery()
End Sub
更新
ERROR
Dim v_Date = DateText.Text
Dim v_Username = UserText.Text
Dim v_Phone = PhoneText.Text
Dim v_Email = EmailText.Text
Dim v_Category = CategoryList.Text
Dim v_Short = ShortText.Text
Dim v_Long = longText.Value
Dim conn As OleDbConnection = New OleDbConnection("Provider=""********"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)
conn.Open()
Dim ClassifiedStr As OleDbCommand = New OleDbCommand("INSERT INTO TABLENAME (Date, Username, Phonenbr, Email, Category, Description, Fulldescription) values(@v_Date, lower(@v_Username), @v_Phone, @v_Email, @v_Category, @v_Short, @v_Long)", conn)
ClassifiedStr.Parameters.Add("@v_Date", OleDbType.Date).Value = v_Date
ClassifiedStr.Parameters.Add("@v_Username", OleDbType.VarChar).Value = v_Username
ClassifiedStr.Parameters.Add("@v_Phone", OleDbType.VarChar).Value = v_Phone
ClassifiedStr.Parameters.Add("@v_Email", OleDbType.VarChar).Value = v_Email
ClassifiedStr.Parameters.Add("@v_Category", OleDbType.VarChar).Value = v_Category
ClassifiedStr.Parameters.Add("@v_Short", OleDbType.VarChar).Value = v_Short
ClassifiedStr.Parameters.Add("@v_Long", OleDbType.VarChar).Value = v_Long
ClassifiedStr.ExecuteNonQuery()
conn.Close()
答案 0 :(得分:1)
您不需要使用数据适配器,因为您没有返回并将数据返回给客户端,而是使用ExecuteNonQuery()
,所以类似(添加了参数化):
Protected Sub Insert_Click(sender As Object, e As EventArgs) Handles Insert.Click
Using conn As New OleDbConnection("Provider=""*********"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)
conn.Open()
Using ClassifiedStr As New OleDbCommand("INSERT INTO t_classifieds (""Date"", Username, Phonenbr, Email, Category, Description, Fulldescription) values(@v_Date, lower(@v_Username), @v_Phone, @v_Email, @v_Category, @v_Short, @v_Long)", conn)
ClassifiedStr.Parameters.Add("@v_Date", OleDbType.Date).Value = DateText.Text
ClassifiedStr.Parameters.Add("@v_Username", OleDbType.VarChar).Value = UserText.Text
ClassifiedStr.Parameters.Add("@v_Phone", OleDbType.VarChar).Value = PhoneText.Text
ClassifiedStr.Parameters.Add("@v_Email", OleDbType.VarChar).Value = EmailText.Text
ClassifiedStr.Parameters.Add("@v_Category", OleDbType.VarChar).Value = CategoryList.Text
ClassifiedStr.Parameters.Add("@v_Short", OleDbType.VarChar).Value = ShortText.Text
ClassifiedStr.Parameters.Add("@v_Long", OleDbType.VarChar).Value = longText.Value
ClassifiedStr.ExecuteNonQuery()
End Using
End Using
End Sub
答案 1 :(得分:1)
Protected Sub Insert_Click(sender As Object, e As EventArgs) Handles Insert.Click
Dim v_Date = DateText.Text
Dim v_Username = UserText.Text
Dim v_Phone = PhoneText.Text
Dim v_Email = EmailText.Text
Dim v_Category = CategoryList.Text
Dim v_Short = ShortText.Text
Dim v_Long = longText.Value
Dim conn As OleDbConnection = New OleDbConnection("Provider=""*********"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)
conn.Open();
Dim ClassifiedStr As OleDbCommand = New OleDbCommand("INSERT INTO t_classifieds ([Date], Username, Phonenbr, Email, Category, Description, Fulldescription) values(@v_Date, lower(@v_Username), @v_Phone, @v_Email, @v_Category, @v_Short, @v_Long)", conn)
ClassifiedStr.Parameters.Add(new OleDbParameter("@v_Date", v_Date))
ClassifiedStr.Parameters.Add(new OleDbParameter("@v_Username", v_Username))
ClassifiedStr.Parameters.Add(new OleDbParameter("@v_Phone", v_Phone))
ClassifiedStr.Parameters.Add(new OleDbParameter("@v_Email", v_Email))
ClassifiedStr.Parameters.Add(new OleDbParameter("@v_Category", v_Category))
ClassifiedStr.Parameters.Add(new OleDbParameter("@v_Short", v_Short))
ClassifiedStr.Parameters.Add(new OleDbParameter("@v_Long",v_Long))
ClassifiedStr.ExecuteNonQuery()
conn.Close()
End Sub
答案 2 :(得分:0)
出于某种原因,INSERT SQL给了我不变的问题所以我决定转而使用存储过程。
使用我在Oracle中的存储过程,以下是我的VB.net代码:
Dim ClassifiedStr As New OleDbCommand
ClassifiedStr.CommandType = CommandType.StoredProcedure
ClassifiedStr.CommandText = "Insert_classifieds"
ClassifiedStr.Connection = conn
'Must be organized based on Stored Procedure
ClassifiedStr.Parameters.Add("val_date", OleDbType.Date).Value = DateText.Text
ClassifiedStr.Parameters.Add("val_category", OleDbType.VarChar, 40).Value = CategoryList.Text
ClassifiedStr.Parameters.Add("val_user", OleDbType.VarChar, 20).Value = UserText.Text
ClassifiedStr.Parameters.Add("val_phone", OleDbType.VarChar, 20).Value = PhoneText.Text
ClassifiedStr.Parameters.Add("val_email", OleDbType.VarChar, 50).Value = EmailText.Text
ClassifiedStr.Parameters.Add("val_shortDes", OleDbType.VarChar, 100).Value = ShortText.Text
ClassifiedStr.Parameters.Add("val_longDes", OleDbType.VarChar, 4000).Value = longText.Value
conn.Open()
ClassifiedStr.ExecuteNonQuery()
conn.Close()