vb.net,我如何获取Field值。 [英] vb.net, How can I grab a Field value.
问题描述
您好,
好的我只有一个问题我无法理解。
我们非常感谢所有帮助。
我正在使用VS 2010和MS Access 2010.
我正在处理的项目将包括MS Access数据库(\\ \\ Database\DB.mdb)
该数据库主要用于跟踪用户信息。
我有一张桌子( UserTable)和一些字段(ID,用户名,密码,活动,级别,注释)
问题是:如何通过搜索UserName获取Level值?
感谢您的阅读,希望您能提供帮助。
编辑:
我想的是:
Private Sub Button1_Click(sender As System.Object ,e As System.EventArgs)处理Button1.Click
Hello,
Ok I got just one question that I can’t quite figure out.
Any and all help is much appreciated.
I am using VS 2010 and MS Access 2010.
The project that I am working on will include a MS Access database (\Database\DB.mdb)
The database will be mainly used to keep track on User information.
I have a Table (UserTable) and some Fields (ID, Username, Password, Active, Level, Notes)
The question is: How can I get the Level value by searching for the UserName?
Thanks for the read, hope you can help.
I was thinking something like:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim dbLevel As String
Using connObj As New SqlClient.SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Database\DB.mdb; Jet OLEDB:Database Password=1qa@WS3ed$RF;") ' testing database
Using cmdObj As New SqlClient.SqlCommand("select Level from UserTable where UserName = 'Test1'", connObj)
connObj.Open()
Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader
'This will loop through all returned records
While readerObj.Read
dbLevel = readerObj("Level").ToString
'handle returned value before next loop here
End While
End Using
connObj.Close()
End Using
End Using
MessageBox.Show("Your variable is: " & dbLevel.ToString)
End Sub
但我不能让它工作,因为我不知道是否:使用cmdObj As New SqlClient.SqlCommand是正确的。
But I cannot get it work as I do no know if: " Using cmdObj As New SqlClient.SqlCommand" is correct.
推荐答案
RF;)' 测试数据库
使用 cmdObj 作为 新 SqlClient.SqlCommand( 从UserTable中选择Level,其中UserName ='Test1',connObj)
connObj.Open()
使用 readerObj 作为 SqlClient.SqlDataReader = cmdObj.ExecuteReader
' 这将遍历所有返回的记录
while readerObj.Read
dbLevel = readerObj( Level)。ToString
' 在下一个循环之前处理返回值这里
结束
结束 使用
connObj.Close()
结束 使用
结束 使用
MessageBox.Show( 你的变量是:& dbLevel.ToString)
结束 Sub
RF;") ' testing database Using cmdObj As New SqlClient.SqlCommand("select Level from UserTable where UserName = 'Test1'", connObj) connObj.Open() Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader 'This will loop through all returned records While readerObj.Read dbLevel = readerObj("Level").ToString 'handle returned value before next loop here End While End Using connObj.Close() End Using End Using MessageBox.Show("Your variable is: " & dbLevel.ToString) End Sub
但我不能让它工作,因为我不知道是否:使用cmdObj As New SqlClient.SqlCommand是正确的。
But I cannot get it work as I do no know if: " Using cmdObj As New SqlClient.SqlCommand" is correct.
将SELECT语句从现在改为....
Change your SELECT statement from what it is now to ....
Using cmdObj As New SqlClient.SqlCommand("select Level, Username from UserTable where UserName = 'Test1'", connObj)
以及在WHILE / END WHILE中的声明....
And also the statement INSIDE the WHILE / END WHILE to....
dbLevel= readerObj.GetValue(readerObj.GetOrdinal("Level"))
只要您的数据库字段命名为Level和Username即可你会没事的。
As long as your database fields are named Level and Username you should be fine.
这篇关于vb.net,我如何获取Field值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!