菜鸟求助~vb中运行insert语句添加数据信息到数据库中的代码。。。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim bookid, bookname, bookauthor, bookpublisher, bookpublishidate, bookclassid As String
bookid = txtbookid.Text
bookname = txtbookname.Text
bookauthor = txtbookauthor.Text
bookpublisher = txtbookpublisher.Text
bookpublishidate = txtbookpublishidate.Text
bookclassid = txtbookclassid.Text
Dim con, com As String
con = "data source=.;database=library;uid=sa;pwd="
com = "insert into bookinfo values('" & Trim(bookid) & "','" & Trim(bookname) & "','" & Trim(bookauthor) & "','" & Trim(bookpublisher) & "','" & Trim(bookpublishidate) & "','" & Trim(bookclassid) & "')"
Dim sqlcon As SqlConnection = New SqlConnection(con)
Dim sqlcom As SqlCommand = New SqlCommand(com)
sqlcom.Connection = sqlcon
sqlcon.Open()
sqlcom.ExecuteNonQuery()
sqlcon.Close()
End Sub
程序可成功运行,但在点击添加按钮后出现sqlcom.ExecuteNonQuery()提示这句话有错误。。
提示信息是:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。
这个要怎么处理, --------------------编程问答-------------------- con = "data source=.;database=library;uid=sa;pwd="
连接字符串写完整了看看还会有错误码?
如果还有断点检查你的sql语句是否有未闭合的符号,赋值是否正确 --------------------编程问答-------------------- 还是出错。有没有人有完整的代码insert添加数据到数据库的代码。。。谢谢了
--------------------编程问答-------------------- 断点运行,将SQL语句拷出来,
放到数据库中使用。。可以知道是否是SQL语句 --------------------编程问答-------------------- 我直接把代码贴给你吧. 可以参照一下.
Public Connection As OleDb.OleDbConnection = Nothing
Public myTrans As OleDb.OleDbTransaction = Nothing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable
Dim i As Integer
Dim strSQL As String
strSQL = " INSERT INTO USERMST (USERID, USERNM, SHORTNAME, "
strSQL += " SYSTAG, PASSWORD, RIGHTID) "
strSQL += " VALUES ( 'aaa','1' ,'11' ,'111' ,'11','11')"
i = Me.UPDateSQL(strSQL)
If i = 1 Then
MessageBox.Show("INSERT SUCCESS!")
Else
MessageBox.Show("INSERT FIELD!")
End If
End Sub
Public Function FindBySQL(ByRef statement As String) As DataTable
Dim command As OleDb.OleDbCommand = New OleDb.OleDbCommand(statement, Connection)
Dim datatable As DataTable = New DataTable
If Not (myTrans Is Nothing) Then
command.Transaction = myTrans
End If
Dim dt As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(command)
Try
dt.Fill(datatable)
Return datatable
Catch e As Exception
Throw e
Finally
command.Dispose()
command = Nothing
End Try
End Function
Public Function UPDateSQL(ByVal sqlString As String) As Integer
Dim command As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlString, Connection, myTrans)
Try
UPDateSQL = command.ExecuteNonQuery()
Catch e As Exception
Throw e
Finally
command.Dispose()
command = Nothing
End Try
End Function
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim CONNECTION_STRING As String
CONNECTION_STRING = " Provider=msdaora.1;Password=KOKUYO;Persist SecurityInfo=True;User ID=KOKUYO;Data Source=PDORA;OLE DBServices=-4"
Connection = New OleDb.OleDbConnection(CONNECTION_STRING)
Connection.Open()
myTrans = Connection.BeginTransaction()
End Sub
补充:.NET技术 , VB.NET