這是添加記錄用vb.net SQL表的一個非常簡單的任務,但我掙扎。添加記錄到表使用VB.Net使用SQLParameters
Dim query As String = String.Empty
query &= "INSERT INTO StudentInfo(StudentName,StudentPhone) VALUES(@param1,@param2)"
Using conn As New SqlConnection("csStudentDB")
Using comm As New SqlCommand()
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
.Parameters.AddWithValue("@StudentName", txtStudentName.Text)
.Parameters.AddWithValue("@StudentPhone", txtStudentPhone.Text)
End With
Try
conn.Open()
comm.ExecuteNonQuery()
Catch ex As Exception
End Try
End Using
End Using
Web.config文件:
<connectionStrings>
<add name="csStudentDB" connectionString="Data Source=xxxxx;Initial Catalog=xxxxx;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
我想將記錄添加到我的桌子,但我得到的錯誤的錯誤在Using conn As New SqlConnection(....."
「的格式是發生初始化字符串不符合從索引0開始的規範 。「
的任何解決方案,將不勝感激。
你應該看看(http://blogs.msmvps.com/jcoehoorn /博客/ 2014/05/12 /可任意使用我們-addwithvalue-已停止/),並停止使用'.AddWithValue()' - 它可能會導致意外和令人驚訝的結果... – 2014-10-07 11:13:06