private void button1_Click(object sender, EventArgs e)
{
DataTable DataTab = new DataTable();
DaSql = new SqlDataAdapter("SELECT * FROM Student where Gender = '" + textBox1.Text + "' ", conSql);
DaSql.Fill(DataTab);
DataGridQueryResult.DataSource = DataTab;
}
在运行代码之前,不要过多地关注SQL注入。让它工作,然后保护它。尝试将变量设置为
var sqlText = "SELECT * FROM Student where Gender = '" + textBox1.Text + "' ";
然后在调试器中点击它来检查完整的查询语句。确保你没有任何愚蠢的事情,比如额外的空间。
请参阅此处填写数据表的完整示例
使用参数修复SQL注入漏洞
Query没有任何错误,似乎与DataGrid存在绑定问题。我的情况也是一样,我使用SQL、DataTable、DataGrid。你会试试这个吗?
DataTable DataTab = new DataTable("Student");
DaSql = new SqlDataAdapter("SELECT * FROM Student where Gender = '" + textBox1.Text + "' ", conSql);
DaSql.Fill(DataTab);
DataGridQueryResult.ItemsSource = DataTab.DefaultView;
并且需要检查窗口设计器中的DataGrid设置是否正确,包括AutoGeneratingColumns=True。我总是使用ItemsSource,结果很好。