为什么结果会保持返回0?

时间:2017-06-17 06:25:37

标签: c# asp.net database parameters

我是c#ASP.NET的新手,我想用登录部分的值insert检查数据库值,我正确地做了一切但为什么我仍然继续得到值不正确,而我输入相同的值与我的数据库中的一个...任何想法?并且我的行计数为dt继续变为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 = "";
                }
        }

2 个答案:

答案 0 :(得分: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=@useremail and password=@password", 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 = "";
                }
        }

答案 1 :(得分: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=@useremail and password=@password", 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" />