带有参数的C#mysql执行读取器输入字符串的格式不正确
我正在尝试创建一个登录aspx页面 我做错了什么带有参数的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
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提供程序,请参见此链接。在这里,您会发现一条注释:提供程序的早期版本使用了“@”符号来标记参数。新版本的使用?相反