2017-11-11 110 views
-1

當我有我的登錄按鈕按下我得到這個錯誤我在Visual Basic 2017年做一個登錄頁面,我的C#的形式得到一個錯誤每次我嘗試用正確的憑據登錄

Error Picture

我的代碼:

private void button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection sqlcon = new SqlConnection (@"C:\LOGIN\DB\LOGINDB.MDF"); 
    string query = "Select * from Table where username='" + txtUsername.Text + "'and password='" + txtPassword.Text + "'"; 
    SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon); 
    DataTable dtbl = new DataTable(); 
    sda.Fill(dtbl); 
    if(dtbl.Rows.Count == 1) 
    { 
     frmMain objFrmMain = new frmMain(); 
     this.Hide(); 
     objFrmMain.Show(); 
    } 
    else 
    { 
     MessageBox.Show("Check you things"); 
    } 
} 

和我的SQL表:

Main table

and this

+0

一個不安全的數據庫,SQL注入攻擊就緒代碼*和*存儲的純文本憑據? – alroc

+0

您是否調試過代碼?你有沒有檢查你得到的錯誤?你能在這裏分享錯誤信息嗎? –

+0

用try/catch包圍年代碼,並在年度問題plz中包含例外。 –

回答

0

假設你的路徑是正確的。 您的連接強應該有數據源等你的格式是完全錯誤的

Data Source=.\SQLExpress;Integrated Security=true; 
AttachDbFilename=C:\MyFolder\MyDataFile.mdf;User Instance=true; 

更多的連接字符串是在這裏 connection string guide 後聲明烏爾SQL連接。 您需要打開您的連接

Sqlcon.open();

+0

我將這些更改添加到我的代碼中: SqlConnection sqlcon = new SqlConnection(@「Data Source =。\ SQLExpress; AttachDbFilename = C:\ LOGIN \ DB \ LOGINDB.MDF; Integrated Security = true; Connect Timeout = 30; User Instance = True;「); 並添加了sqlcon.Open();在if部分 – Transformerio

+0

然後我得到這個錯誤:System.Data.SqlClient.SqlException:'建立到SQL Server的連接時發生網絡相關或實例特定的錯誤。服務器未找到或無法訪問。驗證實例名稱是否正確,並將SQL Server配置爲允許遠程連接。 (提供程序:SQL網絡接口,錯誤:26 - 錯誤定位服務器/實例指定)' – Transformerio

+0

取出sqlcon。在調用連接到數據庫之前從if部分打開。如果你把內部的if部分。那時候你的連接還沒有打開。你期望它如何檢索數據? –

相關問題