C#做的登陆窗口 出错 下面是操作数据库的代码

来源:百度知道 编辑:UC知道 时间:2024/05/13 08:59:01
string str = "server=(local);database=work;user id=sa;pwd=";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string name = textBox1.Text;
string password = textBox2.Text;
SqlCommand sql = new SqlCommand();
sql.CommandText = "select * from user where u_name=name and u_password=password";
sql.Connection = conn;
sql.ExecuteNonQuery();

报错信息是:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 System.Data.dll 中。

其他信息: 在关键字 'user' 附近有语法错误。
加上【】 就说
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 System.Data.dll 中。

其他信息: 列名 'name' 无效。
列名 'password' 无效。

是不是string name = textBox1.Text;
string password = textBox2.Text;
不能这么样让 数据库里的列等于name和password啊?

sql.CommandText = "select * from [user] where u_name='"+name+"' and u_password='"+password+"'";

你在试试

user 表名写错了吧

user id 好像不应有空格吧
server={}里边的单词好像错了

'user' 是关键字,应该是[user]

我记得 我做这个时候 pwd是写的 不是空着的