2014-01-09 199 views
0

如何将行数据转换为字符串或文本并将其显示到标签中?我的问题是,当我点击我的登录按钮,其中包含获取行数据到标签的SQL代码,我的标签中的结果是错误的。而不是文字。我怎样才能将它转换成字符串?将数据转换为字符串转换vb.net 2010

这里是我的代码:

所有的
Private Sub cmdLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLog.Click 
     Dim connection As New SqlClient.SqlConnection 
     Dim command As New SqlClient.SqlCommand 
     Dim adaptor As New SqlClient.SqlDataAdapter 
     Dim dataset As New DataSet 
     Dim reader As MySqlDataReader = Nothing 
     Dim sapi 
     sapi = CreateObject("sapi.spvoice") 

     connection.ConnectionString = ("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Calupad\Desktop\HTF feat Yiyet\HTF feat Yiyet\Database1.mdf;Integrated Security=True;User Instance=True") 
     command.CommandText = "SELECT * FROM [Users] WHERE Username='" & txtUser.Text & "' AND Password ='" & txtPass.Text & "';" 
     txtWel.Text = "Welcome Back, " + txtUser.Text + "!....." 


     connection.Open() 
     command.Connection = connection 
     adaptor.SelectCommand = command 
     adaptor.Fill(dataset, "0") 

     txtStat.text = command.CommandText = "SELECT Status FROM [Users] WHERE Username = '" & txtUser.Text & "' ".ToString 

     txtStat.Text = stat 
       Dim count = dataset.Tables(0).Rows.Count 
     If count > 0 Then 

      MsgBox("Login Successful!" & vbNewLine & txtStat.Text, MsgBoxStyle.Information, "Access Granted") 
      sapi.speak(txtWel.Text) 
      Me.Hide() 
      Form1.Show() 
      frmMenu.Show() 
      txtUser.Clear() 
      txtPass.Clear() 
      tries = 3 
     Else 
      ctr = tries - 1 
      tries = ctr 
      sapi.speak(txtUser.Text + txtNot.Text) 
      MsgBox("Invalid Account!" + vbNewLine + "Attempts Remaining: " & tries, vbCritical, "Access Denied") 
      txtUser.Clear() 
      txtPass.Clear() 
      If tries = 0 Then 
       MsgBox("You've reached the maximum attempts!" + vbNewLine + "The program will be terminated.", vbCritical, "Terminated!") 
       Me.Close() 
      End If 
     End If 
    End Sub 
+0

什么是'stat'在这里:'txtStat.Text = stat'?我们看不到它在代码中的任何地方声明 – har07

+0

stat是我的字符串变量:)我已经声明它 – user3172293

+0

我会再次粘贴我的代码。等待:) – user3172293

回答

1

首先,检查用户名和密码是弱,是肯定volnurable到SQL注入的方式。您正在检查行的'count'是否大于零,然后用户已成功登录,因为您应该只将count与1进行比较,而不是对行进行计数,请尝试将行值与用户具有的值进行比较输入用户名和密码字段以及数据库行返回的内容。

“黑客”可以简单地输入这一点,他将被允许根据你的代码的逻辑登录:

SQL INJECTION

你只需要检索存储dataset变量中的数据,你使用适配器填充。

假设你的数据库表中包含像First_Name领域和“姓氏”,这里是你怎么能在你的窗体上的任何标签控件显示它们:

adaptor.Fill(dataset, "0") 
myFirstName.Text = dataset.Tables(0).Rows(0).Item("First_Name").ToString() 
myLastName.Text = dataset.Tables(0).Rows(0).Item("First_Name").ToString() 

您也可以检索列,而不必知道它的命名这样

myLabel.text = = dataset.Tables(0).Rows(0).Item(3).ToString() 
    'This will retrieve the 4th column from the table (zero based array) 

您还可以通过声明一个变量清理你的代码保存检索到的表

adaptor.Fill(dataset, "0") 
Dim myTable as DataTable = dataset.Tables(0) 
myFirstName.Text = myTable.Rows(0).Item(0).ToString() 

希望这会有帮助

+0

谢谢,但是,我想要做的是获得与登录的用户相同的状态,状态已存储在数据库中有3列为用户名,密码和状态的表。 – user3172293

+0

@ user3172293我修改了我的答案,提醒您注意SQL注入。状态变量的声明在哪里? – Ahmad

+0

谢谢你的帮助,代码运行良好:)上帝保佑你,真的! – user3172293