Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有参数的C#mysql执行读取器输入字符串的格式不正确_C#_Asp.net_Mysql - Fatal编程技术网

带有参数的C#mysql执行读取器输入字符串的格式不正确

带有参数的C#mysql执行读取器输入字符串的格式不正确,c#,asp.net,mysql,C#,Asp.net,Mysql,我正在尝试创建一个登录aspx页面 我做错了什么 MySqlConnection cn = new MySqlConnection("Server=localhost;Database=securitytest; User=root;Password=sx;"); cn.Open(); MySqlCommand cmd = new MySqlCommand("Select * from login where username=@username and password=@p

我正在尝试创建一个登录aspx页面

我做错了什么

MySqlConnection cn = new MySqlConnection("Server=localhost;Database=securitytest; User=root;Password=sx;");

    cn.Open();
    MySqlCommand cmd = new MySqlCommand("Select * from login where username=@username and password=@password", cn);

    //Add parameters to get the username and password  

    cmd.Parameters.Add("@username", OdbcType.VarChar);
    cmd.Parameters["@username"].Value = this.Login1.UserName;

    cmd.Parameters.Add("@password", OdbcType.VarChar);
    cmd.Parameters["@password"].Value = this.Login1.Password;

    MySqlDataReader dr = default(MySqlDataReader);
    // Initialise a reader to read the rows from the login table.  
    // If row exists, the login is successful  

    dr = cmd.ExecuteReader();

    if (dr.HasRows)
    {
        e.Authenticated = true;
        // Event Authenticate is true  
    }

MySql数据库提供程序使用在SQL中定位参数。因此,使用而不是@来标记SQL查询中的参数:

MySqlCommand cmd = new MySqlCommand("Select * from login where username=?username and password=?password", cn);

cmd.Parameters.Add("?username", OdbcType.VarChar);
cmd.Parameters["?username"].Value = this.Login1.UserName;

...

希望,这会有所帮助。

@Mike:我指的不是ODBC提供程序。有关MySQL.Net提供程序,请参见此链接。在这里,您会发现一条注释:提供程序的早期版本使用了“@”符号来标记参数。新版本的使用?相反