在线等,一个查询时出现的问题
//按姓名查找
public DataSet CXXXXM(string xm)
{
//创建数据库连接
SqlConnection conn = new SqlConnection( "server=localhost;Initial catalog =LIS;uid=sa;password=; ");
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter( "SELECT * FROM BRXX WHERE XM= " +xm, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet ds = new DataSet();
adapter.Fill(ds, "BRXX ");
return ds;
conn.Close();
}
运行时在adapter.Fill(ds, "BRXX ");中出现“将 varchar 值 '王五 ' 转换为数据类型为 int 的列时发生语法错误。”的错误是怎么回事?我在数据库中根本就没有定义INT型的啊,但我把姓名“王五”换成数字就可以查询到结果?为什么啊?
------解决方案--------------------SqlDataAdapter adapter = new SqlDataAdapter( "SELECT * FROM BRXX WHERE XM= " +xm, conn);
=〉
SqlDataAdapter adapter = new SqlDataAdapter( "SELECT * FROM BRXX WHERE XM= ' " +xm+ " ' ", conn);
加上单引号试一试。这样才表示字符串