谁能帮我解决VB.net登录代码 [英] Can any one please help me in solving the VB.net Login code
问题描述
嗨
我正在VB.net 2008上工作,但尚未在vb.net上工作,所以请帮助我
我在登录时遇到问题
我写的代码是:
Hi
I am working on VB.net 2008 i have not worked in vb.net so please help me
i have facing issue in login
The code i have written is :
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
''create connection to the database server
Dim con As New SqlClient.SqlConnection
Dim strcon As String = "data source=aarti-pc\sqlexpress.Invoice.dbo"
Dim strcommand As String = "select * from login"
Try
Dim cm As New SqlClient.SqlConnection(strcommand, con)
con.Open()
cm.Executenonquery()
MsgBox.show("You have Log In!!")
Catch ex As Exception
MsgBox.Show(ex.Message, "Error")
Finally
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Sub
请谁能更正我的代码
在此先感谢:)
Please can any one correct my code
Thanks in advance :)
推荐答案
如何纠正?它没有任何用处.
它所要做的就是显示一个消息框错误"!
这里有很多错误:
1)您不使用连接字符串来连接数据库.
2)如果确实有效,则尝试读取所有登录表记录.假设并没有真正失败,那么您假设用户可以登录.
3)您尝试读取所有记录,但是却丢弃了所有数据...
返回您的课程笔记,这次阅读它们.正确!
How can we correct it? It does nothing useful.
All it will do is show a message box "Error"!
There are many things wrong here:
1) You don''t use the connection string to connect to the database.
2) If that did work, you try to read all the login table records. Provided that doesn''t actually fail, you assume the user can log in.
3) You try to read all the records, but you throw away all the data...
Go back to your course notes, and read them this time. Properly!
以下格里夫斯回答,您不能使用
查询数据库
Following Griffs answer you cant query the database with
Dim cm As New SqlClient.SqlConnection(strcommand, con)
您将像这样使用sqlcommand对象
you would use the sqlcommand object like so
Public Sub CreateCommand(ByVal queryString As String, _
ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
command.Connection.Open()
command.ExecuteNonQuery()
End Using
End Sub
此示例摘自 SQL连接类 [ ^ ]
另外,当您执行非查询时,您不会从数据库中返回结果集.即,除了返回的行数之外,没有结果将被返回.看看 SQL数据读取器类 [
this example was taken from SQL Connection Class[^]
Also your not returning a resultset from the database as you are executing a non query .i.e no results are to be return except the number of rows that are return. Have a look at SQL Data Reader Class[^]
这篇关于谁能帮我解决VB.net登录代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!