用C#实现MySQL参数化查询我有下面的代码(我已经包括了我认为所有相关部分):private String readCommand = "SELECT LEVEL FROM USERS WHERE VAL_1 = ? AND VAL_@ = ?;";public bool read(string id){
level = -1;
MySqlCommand m = new MySqlCommand(readCommand);
m.Parameters.Add(new MySqlParameter("", val1));
m.Parameters.Add(new MySqlParameter("", val2));
MySqlDataReader r = m.ExecuteReader();
if (r.HasRows)
level = Convert.ToInt32(r.GetValue(0).ToString());
r.Close();
return true;}当我运行它时,在添加第一个参数时会得到一个IndexOutOfLimsException。我做错了什么?
3 回答
白猪掌柜的
TA贡献1893条经验 获得超10个赞
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e){ MySqlConnection con = new MySqlConnection("server=localhost;User Id=root;database=result;password=1234"); con.Open(); MySqlCommand cmd = new MySqlCommand("Select * from users where username=?username and password=?password", con); cmd.Parameters.Add(new MySqlParameter("username", this.Login1.UserName)); cmd.Parameters.Add(new MySqlParameter("password", this.Login1.Password)); MySqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows ==true) { e.Authenticated = true; }}
慕虎7371278
TA贡献1802条经验 获得超4个赞
String readCommand = "SELECT LEVEL FROM USERS WHERE VAL_1 = ?param1 AND VAL_2 = ?param2";
m.Parameters.Add(new MySqlParameter("param1", val1));
添加回答
举报
0/150
提交
取消