使用vb.net datatable和datarow功能在sql数据库中插入记录

时间:2013-02-11 05:05:33

标签: sql vb.net

我正在尝试使用vb.net dataadapter,datatable和datarow功能在sql数据库中插入记录。我使用以下代码,但它给了我一个错误:

Object reference not set to an instance of an object

Imports System.Data.SqlClient
Public Class Form1  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=drpractice;Integrated Security=True")
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        Try
            cn.Open()
            da.SelectCommand = New SqlCommand("SELECT * FROM [emp_tbl]", cn)
            da.Fill(ds) 
            Dim dt As New DataTable
            dt = ds.Tables("emp_tbl")

            'Error in this line(Object reference not set to an  instance of an object)'
            Dim dr As DataRow = dt.NewRow() 

            dr.Item("emp_id") = TextBox1.Text.Trim
            dr.Item("emp_name") = TextBox2.Text.Trim
            dr.Item("salary") = TextBox3.Text.Trim
            dr.Item("age") = TextBox4.Text.Trim
            dr.Item("emp_group") = TextBox5.Text.Trim
            dt.Rows.Add(dr)
            da.Update(ds)
            MsgBox("Record Successfully Inserted")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class 

4 个答案:

答案 0 :(得分:0)

检查一下:Dim dr As DataRow = new dt.NewRow()

答案 1 :(得分:0)

你做的一切都很好,但改变了以下几行:

da.Update(ds)

如下:

Dim ESCBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
    ESCBuilder.GetUpdateCommand()
    da.UpdateCommand = ESCBuilder.GetUpdateCommand()
    da.Update(ds)

答案 2 :(得分:0)

根据此处和其他地方的反馈,以下代码对我有用:

    TestDataSet.GetChanges()
    testTableAdapter.Fill(TestDataSet.log_test)
    log_testDataGridView.Refresh()

我需要做的是,创建一个新行,然后获取主键的下一个值(VARCHAR,NOT INT,因为修订版有一个“R”附加到PK ...不是我的规则..公司的规则。)

我想把刷新放在接近PK的位置,这是我在为新数据路径分配值之后的最后一次,因此它将获得最新的Max +1。

所以,我在查找PK之前,以及在为数据行分配值之后,将其放在PK之外。上面的代码导致数据行空白。所以,我在创建新的DataRow之前就把上面的代码放了。

对于我的代码,这导致代码从SQL表中获取最新数据,然后添加新数据流,最后确定最后一个PK。因为用于填充数据行的数据不在我的表单中,并且没有任何计算,所以代码运行速度足以满足我的需求。我怀疑,如果与我的数据库的连接速度很慢,和/或运行相同进程的人数很多,我会遇到错误,例如重复的PK。

我的答案是将datarow字段值分配给变量,运行刷新,然后将变量分配给字段并立即保存。

也许另一种方法是获取新的PK,然后保存一个空记录,然后填写记录,除了我的表中有足够的字段是必需的,所以我不妨尝试先创建一个空白记录。

答案 3 :(得分:0)

Imports System.Data.SqlClient
Public Class Form4
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim str As String
    Dim count As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
        con.Open()
      ‘  cmd = New SqlCommand("select * from Doctor", con)
        str = "insert into Doctor values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "' )"
        cmd = New SqlCommand(str, con)
        count = cmd.ExecuteNonQuery()
        MessageBox.Show(count & " Record inserted")

       con.close()
    End Sub

Imports System.Data.SqlClient
Public Class Form4
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim str As String
    Dim count As Integer
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
     con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
        con.Open()
        cmd = New SqlCommand("select * from Patient", con)
        cmd = New SqlCommand("Delete from Patient where Name ='" & TextBox1.Text & "'", con)
        cmd = New SqlCommand("Delete from Patient where Address='" & TextBox2.Text & "'", con)
        cmd = New SqlCommand("Delete from Patient where Dieses='" & TextBox3.Text & "'", con)
        cmd = New SqlCommand("Delete from Patient where Patient_no=" & TextBox4.Text & "", con)
‘you can take any row in your program
        count = cmd.ExecuteNonQuery()
        MessageBox.Show("Record Deleted")
    End Sub