2017-06-17 115 views
0

我是新來的c#ASP.NET,我想檢查數據庫值與登錄部分的值插入,我做了一切正常,但爲什麼我仍然繼續得到的值是不正確的,而我輸入相同的值在我的數據庫中的一個......任何想法?我的行數爲dt繼續得到0 ...是否有什麼錯誤,當我添加參數?爲什麼結果返回0?

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Ng\Documents\Visual Studio 2015\Projects\Assignment6\Assignment6\App_Data\photoCompetition.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"); 

protected void Button1_Click(object sender, EventArgs e) 
     { 
       con.Open(); 
       SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE email='@useremail' and password='@password'", con); 
       cmd.Parameters.Add("@useremail", SqlDbType.Text).Value = emailtext.Text; 
       cmd.Parameters.Add("@password", SqlDbType.Text).Value = passwordtext.Text; 
       SqlDataAdapter sda = new SqlDataAdapter(cmd); 
       DataTable dt = new DataTable(); 
       sda.Fill(dt); 
       int i = cmd.ExecuteNonQuery(); 
       con.Close(); 

       if (dt.Rows.Count > 0) 
       { 
        Response.Redirect("Membermenu.aspx"); 
       } 
       else 
       { 
        lblMsg.Text = "Your username and password is incorrect"; 
        lblMsg.ForeColor = System.Drawing.Color.Red; 
        emailtext.Text = ""; 
        passwordtext.Text = ""; 
       } 
     } 

回答

1

當您使用參數,你不需要設置單引號字符串,在查詢中刪除引號

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Ng\Documents\Visual Studio 2015\Projects\Assignment6\Assignment6\App_Data\photoCompetition.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"); 

protected void Button1_Click(object sender, EventArgs e) 
     { 
       con.Open(); 
       SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE [email protected] and [email protected]", con); 
       cmd.Parameters.Add("@useremail", SqlDbType.Varchar).Value = emailtext.Text; 
       cmd.Parameters.Add("@password", SqlDbType.Varchar).Value = passwordtext.Text; 
       SqlDataAdapter sda = new SqlDataAdapter(cmd); 
       DataTable dt = new DataTable(); 
       sda.Fill(dt); 
       int i = cmd.ExecuteNonQuery(); 
       con.Close(); 

       if (dt.Rows.Count > 0) 
       { 
        Response.Redirect("Membermenu.aspx"); 
       } 
       else 
       { 
        lblMsg.Text = "Your username and password is incorrect"; 
        lblMsg.ForeColor = System.Drawing.Color.Red; 
        emailtext.Text = ""; 
        passwordtext.Text = ""; 
       } 
     } 
+0

我已經嘗試,但我得到這個錯誤數據類型varchar和文本在相等的操作符不兼容。 –

+0

什麼是電子郵件和密碼列的數據類型?這是什麼數據庫? MS SQLServer? – Krishna

+0

是varchar和數據庫是MSSQL服務器 –

0

我看起來像是與你的連接字符串,你的代碼工作,而只改變連接串

public class TestSQLConnection 
{ 
     static string sqlConn = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString; 
    SqlConnection con = new SqlConnection(sqlConn); 

    public void TestConnection() 
    { 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("SELECT * FROM [Users] WHERE [email protected] and [email protected]", con); 
     cmd.Parameters.Add("@useremail", SqlDbType.VarChar).Value = "David"; 
     cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = "Fawzy"; 
     SqlDataAdapter sda = new SqlDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     sda.Fill(dt); 
     con.Close(); 
     if (dt.Rows.Count > 0) 
     { 
      Console.WriteLine("Exist"); 
     } 
     else 
     { 
      Console.WriteLine("Not Exist"); 
     } 
    } 
} 

enter image description here

<connectionStrings> 
<add name="TestDB" connectionString="Data Source=localhost;Initial Catalog=TestDB;User ID=sa;Password=xxxx;Integrated Security=False;" 
    providerName="System.Data.SqlClient" />