回 帖 发 新 帖 刷新版面

主题:急召高手

我想编写一个登陆界面,单击command1登陆成功可连接到163上,可是在 flag = cmd.ExecuteScalar处有错误提示:ExecuteScalar: Connection 属性尚未初始化
为什么?

Imports System.data.oledb
Public Class WebForm1
    Inherits System.Web.UI.Page


#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim flag As Integer
        Dim conString As String
        Dim sql As String
        Dim rs As New OleDbConnection
        conString = "provider=Microsoft.Jet.OLEDB.4.0; data source="
        conString += Server.MapPath("111.mdb")
        rs.ConnectionString = conString
        rs.Open()
        Dim cmd As New OleDbCommand
        If Trim(TextBox1.Text) = "" Then
            MsgBox("用户名不能为空,请重新输入!")
            ' TextBox1.SetFocus()
        End If
        sql = "select * from mima where 学号='" & Val(TextBox1.Text) & "'"
        ' rs_login.Open(sql, conn, adOpenKeyset, adLockPessimistic)
        cmd.CommandText = sql
        flag = cmd.ExecuteScalar
        rs.Close()
        If flag > 0 Then
            Response.Redirect("http://www.163.com")

        End If

    End Sub
End Class

回复列表 (共1个回复)

沙发

Dim cmd As New OleDbCommand
只是定义了command对象,并没有告诉他connection是什么,所以加上以下这句来给与
cmd.Connection = rs //rs是你自己建立的connection

我来回复

您尚未登录,请登录后再回复。点此登录或注册