1

我尝试 LINQ VB.NET 但它的工作不插入数据库

这一行“.CommandText = sqlAdd”

消息错误对象引用未设置为对象的实例。

Imports System
Imports System.Configuration
Imports System.Globalization
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text

Public Class _1_1Invoice
    Dim db As New assetDataContext()
    Dim Conn As SqlConnection
    Dim cmd As SqlCommand
    Dim tr As SqlTransaction
    Dim sb As New StringBuilder()

Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click

        If MessageBox.Show("Do you want insert invoice?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            tr = Conn.BeginTransaction()

            sb.Remove(0, sb.Length)
            sb.Append("INSERT INTO tbl_AsInvoice (Poid)")
            sb.Append(" VALUES (@Poid)")
            Dim sqlAdd As String = sb.ToString()

            With cmd
                .CommandText = sqlAdd
                .CommandType = CommandType.Text
                .Connection = Conn
                .Transaction = tr
                .Parameters.Clear()
                .Parameters.Add("@Poid", SqlDbType.VarChar).Value = txtPoid.Text.Trim()
                .ExecuteNonQuery()
            End With
            tr.Commit()
            MessageBox.Show("Succesfull", "Done", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

谢谢你的时间:)

4

2 回答 2

1

cmd没什么。你需要初始化它:

cmd = New SqlCommand()
于 2012-10-13T18:33:13.417 回答
0

您没有初始化cmd因此它是Nothing并且它的属性不能被访问。您必须创建一个新的SqlCommand并将其分配给cmd

cmd = New SqlCommand()
With cmd
    ...
End With
于 2012-10-13T18:34:39.917 回答