Sql server 在SQL Server中插入数据时使用Visual Basic
我将SQL语句放入visual中的按钮,使其在DB中插入数据,当我触摸它时,会发生以下错误: 从字符串“Insert into TBL_Usuario_102 valu”转换为类型“Double”无效 这是按钮中的代码:Sql server 在SQL Server中插入数据时使用Visual Basic,sql-server,vb.net,visual-studio,visual-studio-2017,Sql Server,Vb.net,Visual Studio,Visual Studio 2017,我将SQL语句放入visual中的按钮,使其在DB中插入数据,当我触摸它时,会发生以下错误: 从字符串“Insert into TBL_Usuario_102 valu”转换为类型“Double”无效 这是按钮中的代码: Private Sub Guardar_Click(sender As Object, e As EventArgs) Handles Guardar.Click If NombreDePersona.Text <> "" And Cedula.Text
Private Sub Guardar_Click(sender As Object, e As EventArgs) Handles Guardar.Click
If NombreDePersona.Text <> "" And Cedula.Text <> "" And RepetirContraseña.Text <> "" And Contraseña.Text <> "" Then
If (RepetirContraseña.Text = Contraseña.Text) Then
instruccionSQL = New SqlClient.SqlCommand("Insert into TBL_Usuario_102 values" +
"(" + Cedula.Text + "," +
NombreDePersona.Text + "," + 3 +
"," + Contraseña.Text + "," +
FechaInclusion.Text + "," + 0 +
"," + FechaInclusion.Text + "," + 3 + ")")
MsgBox("Datos Guardados Correctamente")
Cedula.Clear()
NombreDePersona.Clear()
Contraseña.Clear()
RepetirContraseña.Clear()
Else
MsgBox("Las contraseñas no coinciden")
End If
Else
MsgBox("Escriba en Cada Campo")
End If
End Sub
Private Sub-Guardar\u Click(发送方作为对象,e作为事件参数)处理Guardar。单击
如果NombreDePersona.Text“”和Cedula.Text“”以及REPETIRTERCONTRASEña.Text“”和Contraseña.Text“”,则
如果(RepetirContraseña.Text=Contraseña.Text),则
instrucionsql=New SqlClient.SqlCommand(“插入到TBL\u Usuario\u 102值中”+
(“+Cedula.Text+”,”+
NombreDePersona.Text+“,”+3+
“,”+Contraseña.Text+“,”+
FechaInclusion.Text+“,”+0+
“,“+FechaInclusion.Text+”,“+3+”)
MsgBox(“Datos Guardados Correctamente”)
Cedula.Clear()
NombreDePersona.Clear()
相反的尼娜·克利尔()
repetirtracseña.Clear()
其他的
MsgBox(“Las contraseñas no concurren”)
如果结束
其他的
MsgBox(“卡达坎波描述”)
如果结束
端接头
SQL连接在一个模块中,工作正常,因为当我在SQL Server中手动插入数据时,登录工作正常
数据库表中的数据类型按以下顺序排列
创建这样的SQL字符串是危险的,因为它可能导致错误。通常建议使用命令参数;但是,也可以通过将字符串中的单引号加倍来对其进行转义。这将使这种攻击不可能发生。命令参数还有一个优点,即您不必关心字符串(以及转义字符串)、数字、布尔值和日期的格式。例如,见: 现在,您的SQL语句还有另一个问题。字符串必须用单引号括起来。还可以使用
和进行字符串连接。不是+
(就是这个+
,让VB认为您想要添加双倍的)
文本和数字输入的类型似乎与表中的类型不匹配(是NombreDePersona
avarchar(20)
?),您要插入FechaInclusion
两次
我还将明确指定列名
INSERT INTO TBL_Usuario_102 (column_name1, column_name2, ...) values ('a text', 3, ...)
最后,您不执行您的命令。打开连接后:
instruccionSQL.ExecuteNonQuery()
1的可能副本。您的代码很容易受到Sql注入黑客攻击。2.启用选项Strict
。3.VB.Net的连接运算符是&
,而不是+
。由于代码中有+3+
,VB可能试图隐式地将其转换为数字形式,从而导致错误。问题可能是由于在字符串和数字之间使用“+”运算符(例如3)尝试使用字符串串联运算符&或字符串“3”造成的