2014-10-07 55 views
1

這是添加記錄用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開始的規範 。「

的任何解決方案,將不勝感激。

+0

你應該看看(http://blogs.msmvps.com/jcoehoorn /博客/ 2014/05/12 /可任意使用我們-addwithvalue-已停止/),並停止使用'.AddWithValue()' - 它可能會導致意外和令人驚訝的結果... – 2014-10-07 11:13:06

回答

0

試試這個:

With comm 
     .Connection = conn 
     .CommandType = CommandType.Text 
     .CommandText = query 
     .Parameters.AddWithValue("@param1", txtStudentName.Text) 
     .Parameters.AddWithValue("@param2", txtStudentPhone.Text) 
    End With 

因爲在查詢

query &= "INSERT INTO StudentInfo(StudentName,StudentPhone) VALUES(@param1,@param2)" 

你指定參數爲@param1@param2,所以你必須使用samme在.Parameters.AddWithValue

1

在你的INSERT語句您正在使用參數@ param1和@ param2。但是在你的代碼中你正在使用@StudentName和@StudentPhone。他們需要是一樣的。

另外,我還添加了位檢索連接字符串。 例如:

Dim connString As String = String.Empty 

    connString = System.Configuration.ConfigurationManager 
.ConnectionStrings("csStudentDB").ConnectionString 

    Dim query As String = String.Empty 

query = "INSERT INTO StudentInfo(StudentName,StudentPhone) VALUES(@StudentName,@StudentPhone)" 

Using conn As New SqlConnection(connString) 
    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 
+0

錯誤是發生在「使用康恩作爲新的SqlConnection(.....」 – MJH 2014-10-07 11:34:30

+0

好吧,我假設csStudentDb是牽着你的連接字符串變量。我已經更新了答案。 – Donal 2014-10-07 11:36:04

+0

csStudentDB是個名在web.config – MJH 2014-10-07 11:37:30

1

如果一切都失敗了,試試這個:[?我們能否停止使用AddWithValue()已經]

Using conn As New SqlConnection("Data Source=YourDBServerName;Initial Catalog=YourDBName;Integrated Security=True") 
    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 
+0

謝謝,儘管我寧願從web.config文件 – MJH 2014-10-07 12:04:53

+0

得到連接字符串看看這個頁面:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx? CS-保存琅= 1&CS琅= VB#代碼片斷-2 – 2014-10-07 12:19:22

+0

連接屬性是在這裏:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.connection(v = vs.110)的.aspx – 2014-10-07 12:19:40

相關問題