我正在创建学生出勤信息系统。
如您所见,这些方法彼此相关。
students
表有关于每个学生的一般信息。
有些学生总是缺席,所以register
表中没有students_ID。
这导致da.Fill(dt)出错。它说:
MySqlException未被用户代码&#34处理;您的错误 SQL语法;查看与MySQL服务器对应的手册 正确使用语法的版本')'在第1行"
如何消除此错误?
private string getUquvchilar_ID()
{
var fish = ddlName.Text.Replace("'", "''");
MySqlConnection con = new MySqlConnection(constr);
con.Open();
string query = "Select Students_ID from students where CONCAT(Familiyasi , ' ', Ismi , ' ', Sharifi)='" + fish + "'";
MySqlCommand cmd = new MySqlCommand(query);
cmd.Connection = con;
String s = (cmd.ExecuteScalar() ?? String.Empty).ToString();
con.Close();
return s;
}
private void BindGrid()
{
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM rasmlar where Rasm_ID in (" + String.Join(",", getImage_ID()) + ")", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
}
private List<int> getImage_ID()
{
List<int> i = new List<int>();
MySqlConnection con = new MySqlConnection(constr);
con.Open();
string query = "Select bosh_rasm_ID, Tugal_rasm_ID from register where students_ID='" + getUquvchilar_ID() + "'";
MySqlCommand cmd = new MySqlCommand(query);
cmd.Connection = con;
MySqlDataReader reader = cmd.ExecuteReader();
if (reader != null && reader.HasRows)
{
foreach (DbDataRecord s in reader)
{
if (!reader.IsDBNull(reader.GetOrdinal("bosh_rasm_ID")))
i.Add(s.GetInt32(0));
else
i.Add(0);
if (!reader.IsDBNull(reader.GetOrdinal("Tugal_rasm_ID")))
i.Add(s.GetInt32(1));
else
i.Add(0);
}
}
return i;
}
答案 0 :(得分:0)
如果getImage_ID()返回一个空列表,那么select语句将是SELECT * FROM rasmlar where Rasm_ID in ()
...这是一个无效的sql语句......你必须明确处理这个案例。