How can I Insert data into SQL Server using VBNet

前端 未结 3 1923
耶瑟儿~
耶瑟儿~ 2020-11-29 07:52

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

I have tried this

Here I tried Sample Code

相关标签:
3条回答
  • 2020-11-29 08:12
    Function ExtSql(ByVal sql As String) As Boolean
        Dim cnn As SqlConnection
        Dim cmd As SqlCommand
        cnn = New SqlConnection(My.Settings.mySqlConnectionString)
        Try
            cnn.Open()
            cmd = New SqlCommand
            cmd.Connection = cnn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = sql
            cmd.ExecuteNonQuery()
            cnn.Close()
            cmd.Dispose()
        Catch ex As Exception
            cnn.Close()
            Return False
        End Try
        Return True
    End Function
    
    0 讨论(0)
  • 2020-11-29 08:28

    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.

    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.

    ex

    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: Please change the column names specified in the query to the original column found in your table.

    0 讨论(0)
  • 2020-11-29 08:28
    Imports System.Data
    
    Imports System.Data.SqlClient
    
    Public Class Form2
    
    Dim myconnection As SqlConnection
    
    Dim mycommand As SqlCommand
    
    Dim dr As SqlDataReader
    
    Dim dr1 As SqlDataReader
    
    Dim ra As Integer
    
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
        myconnection = New SqlConnection("server=localhost;uid=root;pwd=;database=simple")
    
        'you need to provide password for sql server
    
        myconnection.Open()
    
        mycommand = New SqlCommand("insert into tbl_cus([name],[class],[phone],[address]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')", myconnection)
    
        mycommand.ExecuteNonQuery()
    
        MessageBox.Show("New Row Inserted" & ra)
    
        myconnection.Close()
    
    End Sub
    
    End Class
    
    0 讨论(0)
提交回复
热议问题