【发布时间】:2015-05-08 05:46:30
【问题描述】:
我有这个模块调用过程,我想参数化它。我正在向过程模块发送一个字符串作为查询。我已经在 google 中查找,但找不到问题的答案。
Procedures.Insert("INSERT INTO Technician (tec_name, tec_email, rol_id) VALUES ('" & txt_tech.text & "', '" & txt_tech_email.text & "', " & cbo_tech_role.selectvalue.tostring & ")", "Technican Add Correct")
========================================== 我可能会改变它......
Procedures.Insert("INSERT INTO Technician (tec_name, tec_email, rol_id) VALUES ('@tech_name', '@tech_email', '@tech_role' ")", "Technican Add Correct")
================ 但是我不知道在哪里可以参数化
Public Sub Insert(query As String, msg As String)
Dim cn As New SqlConnection(cs)
Dim cmd As New SqlCommand
Try
cn.Open()
With cmd
.CommandType = CommandType.Text
.CommandText = query
.Connection = cn
.Parameters.AddValueWith("@tech_name",txt_tech_name.text)
.Parameters.AddValueWith("@tech_email",txt_tech_email.text)
.Parameters.AddValueWith("@tech_rol",txt_tech_role.selectValue.tostring)
.ExecuteNonQuery()
End With
MessageBox.Show(msg, "INSERT", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, ". : : ERROR : : .", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If cn IsNot Nothing AndAlso cn.State <> ConnectionState.Closed Then
cn.Close()
cn = Nothing
End If
End Try
End Sub
因为我有一个与主代码分开的模块,所以我无法调用文本框,因为它们与主模块分开......关于如何做到这一点的任何想法? ...别太难了..这是我使用 VB 的 14 周.. :/
【问题讨论】:
标签: sql vb.net parameterized