vb.net正确的函数数据表对象和引用未设置为对象的权限

时间:2014-08-29 13:46:22

标签: vb.net winforms

我是新来的,也是在vb.net 我想问一下在所有形式的项目中获取数据表的正确方法是什么 例如,我已经制作了模块。

Imports System.Data.SqlClient
Imports System.IO

Module ModBack
    Public dt As New DataTable
    Dim vConnectionString As String
    Dim line As String

    Public Sub iDT(vSql As String)
        Try
            Dim v As String = System.AppDomain.CurrentDomain.BaseDirectory() & "connection.config"
            'Dim sr As StreamReader = New StreamReader("E:\Ipc New\IPC MOBILE\JoMobile\connection.config")
            Dim sr As StreamReader = New StreamReader(v)
            line = sr.ReadLine()
            Console.WriteLine(line)
            sr.Close()
        Catch E As Exception
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(E.Message)
        End Try
        'vConnectionString = "Data Source=KHAN\SQLEXPRESS;Initial Catalog=JoMobile;Trusted_Connection=YES;"
        vConnectionString = line & "Uid=*;Pwd=*;"
        'providerName = "System.Data.SqlClient"
        Try
            Dim cnn As New SqlConnection(vConnectionString)
            cnn.Open()
            Dim da As New SqlDataAdapter(vSql, cnn)
            dt.Columns.Clear()
            dt.Rows.Clear()
            da.Fill(dt)
            cnn.Close()
        Catch ex As Exception
            MessageBox.Show("Error:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

现在我从

这样的表格中调用它
Try
    iDT("SELECT * FROM Subjects")
    Dim dt1 = dt.Copy()
    If dt1.Rows.Count > 0 Then
        cmbSubject.DataSource = dt1
        cmbSubject.ValueMember = "SID"
        cmbSubject.DisplayMember = "Subject"
    End If
    cmbSubject.SelectedIndex = 0
Catch ex As Exception
    MessageBox.Show("Error:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
End Try

有时我会得到错误:对象引用未设置为对象的权限。 请帮我教简单的方式thx

0 个答案:

没有答案