2012-09-28 132 views
13
数据到SQL Server

我是新来vb.net我需要使用插入表中的数据vb.net请任何一个可以帮助我怎样才能插入使用VBNet

我已经试过这

在这里,我试过示例代码

我得到这个例外Column name or number of supplied values does not match table definition. 感谢推进

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 
+0

您应该向我们展示_table definition_。也许你忘记了自动增加pk列'IDENTITY'。顺便说一句,你打开SQL注入。您应该使用['SqlParameters'](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlparameter.aspx)。 –

+0

我的桌子'defn'是'学生(SName,SID,SPhone,SBranch,SCourse,SFee)' 所有属于'varchar'类型,除了'fee'('费用是实际类型') – rangasathish

+1

@rangasathish - 那不是一个表格定义。最简单的方法是使用SQL Server Management Studio(SSMS)访问表格。右键单击它,选择“脚本表 - >作为创建 - >到剪贴板”。然后*编辑*你的问题并粘贴结果。它会向我们显示表格的*数据类型*,任何*约束*等。 –

回答

16

这意味着,值的数量specifie INSERT语句中的VALUES子句中的d不等于表中的列总数。如果您只尝试插入选定的列,则必须指定列名。

另一个问题,由于您使用的是ADO.Net,因此请始终参数化您的查询以避免SQL Injection。你现在正在做的是你击败使用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:请修改查询中指定到你的表中找到的原始列的列名。

+0

注意:'conn.Close'是多余的,因为您无论如何都使用'using-statement'。 –

+0

@TimSchmelter感谢提醒:)但我曾经这样做。无论如何,我只是更新我的答案。 –

+2

我会投票+2,如果我可以为参数化查询的促进 – jeroenh

4
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