注册 登录
编程论坛 VB.NET论坛

请大神帮我看一下这个代码那里有错

zhongsw520 发布于 2018-02-22 14:46, 2113 次点击
Imports System.Data.SqlClient

Public Class Form1
    Dim conn As New SqlConnection()
    Dim cmd As New SqlCommand
    Dim datard As SqlDataReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim constr As String '连接数据源
        constr = "user id=sa;password=123456;initial catalog=rsgl;data source=(local)"
        conn.ConnectionString = constr
        conn.Open()
        cmd.Connection = conn  '命令对像连接
         = CommandType.Text
        Dim cmdstr As String  '定义连接查询语句
        cmdstr = "select * from 用户表 "
         = cmdstr
        Dim datard As SqlDataReader  '定义读取方式
        datard = cmd.ExecuteReader
        Dim AA As String
        AA = datard.Read
        If datard.Item(CInt("psw")) = TextBox1.Text Then
            'TextBox1.Text = datard.GetString(1)
            TextBox2.Text = datard.GetString(2)
            datard.Close()
            conn.Close()
        Else
            MsgBox("输入不正确")

        End If
2 回复
#2
不懂才问2018-02-22 16:45
If datard.Item(CInt("psw")) = TextBox1.Text Then
    'TextBox1.Text = datard.GetString(1)
    TextBox2.Text = datard.GetString(2)
    datard.Close()
    conn.Close()
Else
    MsgBox("输入不正确")
End If

textbox1.text是字符型,不太可能和Cint("pws")同类型吧?

改成    If datard.Item("psw") = CInt(TextBox1.Text) Then   试试呢?
#3
xyxcc1772018-02-23 16:42
cmdstr = "select * from 用户表 "
上面的语句将返回用户表中的所有记录
AA = datard.Read
上面的写法是错误的,每datard.Read一次,记录指针向下移动一行,如果有记录,返回true,否则返回false.
如果psw是字段名,可以直接写成datard("psw")=

不知道你这段代码要做什么?


[此贴子已经被作者于2018-2-23 16:47编辑过]

1