我在ADP.fill遇到此错误"ConnectionString属性尚未初始化". [英] i m facing this error "The ConnectionString property has not been initialized."at ADP.fill

查看:156
本文介绍了我在ADP.fill遇到此错误"ConnectionString属性尚未初始化".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Imports System.Data.SqlClient
Public Class Department
    Private cs As New SqlConnection("Data Source=(local);Initial Catalog=crystal_employee;Integrated Security=True")
    Private da As New SqlDataAdapter("select * from Department", cs)
    Private ds As New DataSet
    Private cmd As New SqlCommandBuilder(da)

    Dim str As String
    Dim sql As String
    Dim conn As SqlConnection
    Dim cb As SqlCommandBuilder
    Dim ADP As SqlDataAdapter
    Dim tot As Integer
    Dim r As Integer
    Dim dt As New DataTable
    Dim myconnection As New SqlConnection(myconstring)
    Dim mycommand As New SqlCommand
    Dim myconstring As String = "Data Source=(local);Initial Catalog=crystal_employee;Integrated Security=True"
    Public Function executequeryselect(ByVal sql As String) As DataSet
        Try
            ds = New DataSet
            myconnection = New SqlConnection(myconstring)
            da = New SqlDataAdapter(sql, myconnection)
            da.SelectCommand.CommandText = sql
            da.Fill(ds)
        Catch ex As Exception
            MsgBox("Error in  module execute query select" & ex.Message.ToString)
            Debug.Write(ex.Message.ToString)
        End Try
        Return ds
    End Function
    Public Function executequery(ByVal sql As String) As Integer
        Try
            myconnection = New SqlConnection(myconstring)
            mycommand = New SqlCommand(sql, myconnection)
            mycommand.CommandType = CommandType.Text
            mycommand.Connection.Open()
            Return mycommand.ExecuteNonQuery()
        Catch ex As Exception
            Debug.WriteLine("Error in  module execute query select" & ex.Message.ToString)
        End Try
    End Function
    Public Sub connect()
        Dim myconnection As New SqlConnection(myconstring)
        myconnection.Open()
        Dim mycommand As New SqlCommand
        mycommand.Connection = myconnection
    End Sub
    Public Sub Lock_BTN(ByVal b As Boolean)
        Button1.Enabled = b
        Button2.Enabled = b
        Button3.Enabled = b

    End Sub
   

    Public Sub get_data()
        sql = "select * from Department"
        ADP = New SqlDataAdapter(sql, str)
        dt = New DataTable
        ADP.Fill(dt)
        If dt.Rows.Count > 0 Then
            tot = dt.Rows.Count - 1
            r = 0
        End If
        'disp_data()
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Lock_BTN(True)
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""


        TextBox1.Focus()
        Button2.Text = "&save"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'save
        Dim db As New empclass
        Dim a As New ArrayList
        Dim b As New ArrayList
        'If (Button3.Text = "&Save") Then
        a.Add("@dep_id")
        a.Add("@dep_code")
        a.Add("dep_name")

        b.Add(TextBox1.Text)
        b.Add(TextBox2.Text)
        b.Add(TextBox3.Text)
       
        Button2.Text = "&Save"
        get_data()
        Lock_BTN(True)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()

    End Sub
End Class

推荐答案

您需要设置连接对象
You need to set the connection object
myconnection = New SqlConnection(myconstring)

 ADP = New SqlDataAdapter(sql, str myconnection)


Dim str As String

...
Dim myconstring As String = "Data Source=(local);Initial Catalog=crystal_employee;Integrated Security=True"



您尚未为"str"变量分配值,则应像对"myconstring"变量进行赋值一样分配它

或者,如果您仅使用一个数据库"crystal_employee",则无需声明&使用另一个变量``str''.

祝您编码愉快!
:)



you have not assigned value to ''str'' variable, you should assign it like you have done with ''myconstring'' variable

or if you are using only one database ''crystal_employee'' then no necessary to declare & use another variable ''str''.

Happy Coding!
:)


使用连接之前必须先调用myconnection.Open,并且在SqlDataAdapter(sql, str)中,str必须是连接字符串.
You have to call myconnection.Open before using the connection, and in SqlDataAdapter(sql, str) str must be the connection string.


这篇关于我在ADP.fill遇到此错误"ConnectionString属性尚未初始化".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆