2016-09-19 117 views
-2

我已經在股票這個問題:附加信息:用戶'登錄失敗。 SQL Server 2012的

enter image description here

Imports System.ComponentModel 
Imports System.Data.SqlClient 

Public Class Form1 
    Dim MyConnection As SqlConnection = New SqlConnection("Server=DESKTOP-I0N45MV\SQL2012;Database=user;uid=;pwd=") 
    Dim MyDataAdapter As New SqlDataAdapter() 
    Dim MyDataAdapter1 As New SqlDataAdapter() 
    Dim Result As String 
    Dim Result1 As String 

    Private Sub login_Click(sender As Object, e As EventArgs) Handles login.Click 
     MyDataAdapter.SelectCommand = New SqlCommand() 
     MyDataAdapter1.SelectCommand = New SqlCommand() 

     MyDataAdapter.SelectCommand.Connection = MyConnection 
     MyDataAdapter1.SelectCommand.Connection = MyConnection 
     MyDataAdapter.SelectCommand.CommandText = "Select Username From users WHERE Username ='" & user_.Text & "'" 
     MyDataAdapter1.SelectCommand.CommandText = "Select Password From users WHERE Password ='" & pass_.Text & "'" 
     MyConnection.Open() 
     Result = MyDataAdapter.SelectCommand.ExecuteScalar() 
     Result1 = MyDataAdapter1.SelectCommand.ExecuteScalar() 
     MyConnection.Close() 
+0

您的ConnectionString是不妥當的 –

+0

你是什麼意思> – hotchongas

+0

可以顯示數據庫和應用 –

回答

0

首先曾經使用過這樣的:
MyDataAdapter.SelectCommand.CommandText = "SELECT Username FROM users WHERE Username ='" & user_.Text & "'",因爲它會讓你的代碼是通過SQL注入注射,改爲使用stored procedureparameterized queries

其次我不明白爲什麼你會使用兩個查詢。
難道你不能像SELECT Username FROM users WHERE Username = 'foo' AND Password = 'bar'那樣做,然後就使用這個查詢的結果嗎?

第三我建議你通過連接字符串作爲參數傳遞給您的項目。如果您正在編寫一個WinForms項目,你可以這樣訪問:Dim connection As New SqlConnection(My.Settings.connectionString)

而且最後因爲這是你的要求,你的連接字符串應該像 Server=YourServerName;Database=YourDBName;Trusted_Connection=True;
如果您在SQL Server上使用Windows身份驗證,則應該看起來像這樣遠程Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;,如果您使用用戶名和密碼進行身份驗證。

+0

謝謝哇!你只是救了我的屁股...... – hotchongas

+0

@hotchongas如果這個答案幫助你解決了問題,或者upvote(向上箭頭)或者接受(檢查)它。 – jAC

相關問題