public DataSet[] ShowBackGround(string back_name) { KingbaseESCommand cmd = new KingbaseESCommand(); cmd.Connection = conn; var tables = GetSchemaInfo(back_name); DataSet[] data = new DataSet[tables.Count()]; for (int i = 0; i < tables.Count(); i++) { cmd.CommandText = "select * from " + '"' + back_name + '"' + "." + tables[i]; KingbaseESDataAdapter da = new KingbaseESDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); ds.DataSetName = tables[i]; data[i] = ds; da.Dispose(); } return(data); }
public Tuple <string, bool> RunSql(string sql, string back_name, bool del) { string except = ""; var nback_name = CopyBack("tmp", back_name); KingbaseESCommand cmd = new KingbaseESCommand("SET search_path TO " + '"' + nback_name + '"' + ",public", conn); cmd.ExecuteNonQuery(); cmd.CommandText = sql; DataSet res = new DataSet(); KingbaseESDataAdapter resda; bool flag = true; try { resda = new KingbaseESDataAdapter(cmd); resda.Fill(res); var licount = res.Tables[0].Rows.Count; if (licount == 0) { except = "null"; } else { foreach (var eli in res.Tables[0].Columns) { except += eli.ToString() + " "; } } } catch (System.Exception e) { flag = false; except = e.Message; } var t = new Tuple <string, bool>(except, flag); if (del) { cmd.CommandText = $"DROP SCHEMA \"{nback_name}\" CASCADE"; cmd.ExecuteNonQuery(); } return(t); }
public Tuple <string, bool> JudgeSql(string username, question ques, string ans, string back_name) { ans = ans.Trim(); ans = ans.TrimEnd(';'); string except = null; var nback_name = CopyBack(username, back_name); bool iscorrect = false; KingbaseESCommand cmd = new KingbaseESCommand("SET search_path TO " + '"' + nback_name + '"' + ",public", conn); cmd.ExecuteNonQuery(); cmd.CommandText = ques.right_answer; DataSet right_ds = new DataSet(); if (ques.q_type == 1) { KingbaseESDataAdapter da = new KingbaseESDataAdapter(cmd); da.Fill(right_ds); da.Dispose(); } else if (ques.q_type == 2) { cmd.ExecuteNonQuery(); cmd.CommandText = ques.check_sentence; KingbaseESDataAdapter da = new KingbaseESDataAdapter(cmd); da.Fill(right_ds); da.Dispose(); DeleteBack(nback_name); nback_name = CopyBack(username, back_name); } cmd.CommandText = ans; cmd.CommandTimeout = 5; DataTable ans_dt = new DataTable(); if (ques.q_type == 2) { try { cmd.ExecuteNonQuery(); } catch (System.Exception e) { except = e.Message; iscorrect = false; cmd.CommandText = $"DROP SCHEMA \"{nback_name}\" CASCADE"; cmd.ExecuteNonQuery(); return(new Tuple <string, bool>(except, iscorrect)); } cmd.CommandText = ques.check_sentence; } else { cmd.CommandText = ans + " limit 100"; } //KingbaseESDataReader dr = null; int count = 0; try { /* * dr = cmd.ExecuteReader(); * for (var i = 0; i < dr.FieldCount; i++) { * DataColumn column = new DataColumn(); * column.DataType = dr.GetFieldType(i); * column.ColumnName = dr.GetName(i); * ans_dt.Columns.Add(column); * } * while (dr.Read() && count<=60) { * DataRow row = ans_dt.NewRow(); * for(var i = 0; i < dr.FieldCount; i++) { * row[i] = dr[i].ToString(); * } * ans_dt.Rows.Add(row); * row = null; * count++; * } * dr.Close(); */ KingbaseESDataAdapter da = new KingbaseESDataAdapter(cmd); da.Fill(0, 100, ans_dt); da.Dispose(); var res = DataTableCompare(ans_dt, right_ds.Tables[0]); if (res.Item1.Length > 0 || res.Item2.Length > 0) { iscorrect = false; if (ques.q_type == 2) { except += $"验证语句{ques.check_sentence}下\n"; } if (res.Item1.Length > 0) { var str = ""; foreach (var item in res.Item1[0].ItemArray) { str += item.ToString().TrimEnd(' ') + ' '; } except += $"比正确结果少{res.Item1.Length}个,如:{str}\n"; } if (res.Item2.Length > 0) { var str = ""; foreach (var item in res.Item2[0].ItemArray) { str += item.ToString().TrimEnd(' ') + ' '; } except += $"比正确结果多{res.Item2.Length}个,如:{str}\n"; } } else { iscorrect = true; except = "congratulations!!"; } } catch (System.Exception e) { except = e.Message; iscorrect = false; } cmd.CommandText = $"DROP SCHEMA \"{nback_name}\" CASCADE"; try { cmd.ExecuteNonQuery(); } catch (System.Exception e) { except = "执行超时"; iscorrect = false; } var re = new Tuple <string, bool>(except, iscorrect); return(re); }