如何使用 VBNet 将数据插入 SQL Server [英] How can I Insert data into SQL Server using VBNet

查看:22
本文介绍了如何使用 VBNet 将数据插入 SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 vb.net 的新手,我需要使用 vb.net 在表中插入数据,请任何人帮忙

I am new to vb.net I need to insert data in table by using vb.net please can any one help

我已经试过了

在这里我尝试了示例代码

Here I tried Sample Code

我收到此异常列名或提供的值数量与表定义不匹配.提前致谢

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs)  Handles btnSave.Click

    Dim strName As String = txtName.Text
    Dim strId As String = txtID.Text
    Dim strPhone As String = txtPhone.Text
    Dim strBranch As String = cmboxBranch.SelectedItem.ToString()
    Dim  strCourse As String = cmbboxCourse.SelectedItem.ToString()
    Dim dblFee As Double = Double.Parse(txtFee.Text) 

    Dim strCommand As String = "insert into student values('" & strName & "','" & strId &    "','" & strPhone & "','" & strBranch & "','" & strCourse & "'," & dblFee & ")"

    Dim command As SqlCommand = New SqlCommand(strCommand, connection)
    command.CommandType = CommandType.Text

    '' MsgBox(strCommand) 

    connection.Open()
    If (command.ExecuteNonQuery().Equals(1)) Then
        MsgBox("Information stored in database")
    Else
        MsgBox("Not stored in database")
    End If

End Sub

推荐答案

这意味着在 INSERT 语句的 VALUES 子句中指定的值数量不相等到表中的总列数.如果您只尝试在选定的列上插入,则必须指定列名.

It means that the number of values specified in your VALUES clause on the INSERT statement is not equal to the total number of columns in the table. You must specify the columnname if you only try to insert on selected columns.

另一个,因为您使用的是 ADO.Net ,所以总是参数化您的查询以避免 SQL 注入.您现在正在做的是打败sqlCommand 的使用.

Another one, since you are using ADO.Net , always parameterized your query to avoid SQL Injection. What you are doing right now is you are defeating the use of sqlCommand.

Dim query as String = String.Empty
query &= "INSERT INTO student (colName, colID, colPhone, "
query &= "                     colBranch, colCourse, coldblFee)  "
query &= "VALUES (@colName,@colID, @colPhone, @colBranch,@colCourse, @coldblFee)"

Using conn as New SqlConnection("connectionStringHere")
    Using comm As New SqlCommand()
        With comm
            .Connection = conn
            .CommandType = CommandType.Text
            .CommandText = query
            .Parameters.AddWithValue("@colName", strName)
            .Parameters.AddWithValue("@colID", strId)
            .Parameters.AddWithValue("@colPhone", strPhone)
            .Parameters.AddWithValue("@colBranch", strBranch)
            .Parameters.AddWithValue("@colCourse", strCourse)
            .Parameters.AddWithValue("@coldblFee", dblFee)
        End With
        Try
            conn.open()
            comm.ExecuteNonQuery()
        Catch(ex as SqlException)
            MessageBox.Show(ex.Message.ToString(), "Error Message")
        End Try
    End Using
End USing 

PS:请将查询中指定的列名更改为在您的表中找到的原始列.

这篇关于如何使用 VBNet 将数据插入 SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆