使用SQLParameters使用VB.Net将记录添加到表中

时间:2014-10-07 11:07:30

标签: asp.net sql-server vb.net

使用vb.net将记录添加到SQL表这是一项非常简单的任务,但我很挣扎。

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(....."发生错误时出现错误

  

&#34;初始化字符串的格式不符合规范   从索引0开始。&#34;

任何解决方案都将受到赞赏。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

如果一切都失败了,试试这个:

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

答案 2 :(得分: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,因此您必须使用.Parameters.AddWithValue中的samme