Sql server 使用Visual Studio向表中添加数据
我想在Visual Studio中将数据添加到我的应用程序表中。当我运行程序时,我将信息填入文本框中,当我单击“添加”按钮时,我收到来自以下代码的消息: Cmd.executenonquery:类型为的未处理异常 System.Data.dll中出现“System.Data.SqlClient.SqlException” 运行生成解决方案时,我收到以下信息: ======生成:0成功,0失败,1最新,0跳过========== 谁能帮帮我吗Sql server 使用Visual Studio向表中添加数据,sql-server,visual-studio,Sql Server,Visual Studio,我想在Visual Studio中将数据添加到我的应用程序表中。当我运行程序时,我将信息填入文本框中,当我单击“添加”按钮时,我收到来自以下代码的消息: Cmd.executenonquery:类型为的未处理异常 System.Data.dll中出现“System.Data.SqlClient.SqlException” 运行生成解决方案时,我收到以下信息: ======生成:0成功,0失败,1最新,0跳过========== 谁能帮帮我吗 Imports System.Data.SqlClie
Imports System.Data.SqlClient
Public Class APP
Dim Con As New SqlConnection
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Con.Open()
Dim Cmd As New SqlCommand(("INSERT INTO App VALUES('" & _
ID.Text & "','" & _
Dat.Text & "','" & _
Inst.Text & "', '" & _
Credent.Text & "', '" & _
AppOwner.Text & "', '" & _
Status.Text & "', '" & _
SerialNr.Text & "', '" & _
MAC.Text & "', '" & _
DellNr.Text & "', '" & _
Model.Text & "', '" & _
Description.Text & _
"', '" & Service.Text & _
"', '" & Indkøbt.Text & "',)"), Con)
Cmd.ExecuteNonQuery()
Con.Close()
MsgBox("Success....", MsgBoxStyle.Information, "SUCCESS")
ID.Clear()
Dat.Clear()
Inst.Clear()
Credent.Clear()
AppOwner.Clear()
Status.Clear()
SerialNr.Clear()
MAC.Clear()
DellNr.Clear()
Model.Clear()
Description.Clear()
Service.Clear()
Indkøbt.Clear()
ID.Focus()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Con.ConnectionString = "Data Source=BYG-A101-MOELKA;Initial Catalog=App;Integrated Security=True"
End Sub
乍一看:
"', '" & Indkøbt.Text & "',)"), Con)
这是错误的,应该是错误的
"', '" & Indkøbt.Text & "')"), Con)
但还有另一个大问题:您没有使用sqlparameters,因此如果有人输入'
当像您这样使用插入时,它也。。。如果在正确的数据库字段中插入值,还指定要查看的字段列表,则可读性更高。但最好改成sqlparameters
Addition-使用update语句添加SQL参数,但原则应明确:
我对这方面真的很陌生。我将在几分钟后发布一个示例,非常感谢您。它现在起作用了,尽管每次运行代码时我都会收到这条消息,但有构建错误。你想继续并运行上一个成功的构建吗?我必须承认,我不知道你的代码是什么意思,也不知道如何使用参数……但至少它的填充数据如果你得到编译错误,你可能没有声明所有变量。
sqlStmt = "UPDATE table SET fieldname = @fieldparameter WHERE ..."
Using sqlComm As New SqlCommand(sqlStmt, sqlConn)
sqlComm.Parameters.Add(New SqlParameter("@fieldparameter", yourvalue))
sqlComm.ExecuteNonQuery()
End Using