【发布时间】:2014-10-08 14:36:23
【问题描述】:
当我尝试在网格视图中显示数据库值时出现错误:
System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常
附加信息:关键字“and”附近的语法不正确。
代码是
private void button1_Click(object sender, EventArgs e)
{
SqlDataAdapter adap;
DataSet ds;
SqlConnection cn = new SqlConnection(
@"Data Source=DILIPWIN\SQLEXPRESS;Initial Catalog=radb;Integrated Security=True");
cn.Open();
var home = new Home();
adap = new SqlDataAdapter(
"select roll_num, mark from marks where mark < 50 and dept_id=" +
home.cboxDept.SelectedValue + " and sem_id=" + home.cboxSem.SelectedValue +
" and subject_id=" + home.cboxSubject.SelectedValue + " and batch_id= " +
home.cboxBatch.SelectedValue + " and cls_id=" + home.cboxClass.SelectedValue, cn);
ds = new System.Data.DataSet();
adap.Fill(ds, "dataGridView1");
dataGridView1.DataSource = ds.Tables[0];
}
【问题讨论】:
-
你能在所有连接后发布查询吗?
-
我假设您正在使用 WinForms 并且实际上正在使用 DataGridView?请始终适当地标记,否则你会浪费 WPF 人的时间! (反之亦然..)
标签: c# sql sql-server gridview