C#中用三种形式都有错。SqlDataReader reader = sqlCommand.ExecuteReader();
SqlConnectionmyConnection=newSqlConnection();myConnection.ConnectionString="server=lo...
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "server=localhost;integrated security=SSPI;database=newspaper";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = myConnection;
myConnection.Open();
sqlCommand.CommandText = "select users.uNO,upwd from users where uNO.='" + this.textBox1.Text.Trim() + "'and upwd='" + this.textBox2.Text.Trim() + "'";
sqlCommand.CommandType = CommandType.Text;
if (reader.Read())
{
this.Hide();
users user = new users ();
user.Show();
} 展开
myConnection.ConnectionString = "server=localhost;integrated security=SSPI;database=newspaper";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = myConnection;
myConnection.Open();
sqlCommand.CommandText = "select users.uNO,upwd from users where uNO.='" + this.textBox1.Text.Trim() + "'and upwd='" + this.textBox2.Text.Trim() + "'";
sqlCommand.CommandType = CommandType.Text;
if (reader.Read())
{
this.Hide();
users user = new users ();
user.Show();
} 展开
2个回答
展开全部
using(var myConnection = new SqlConnection())
{
myConnection.ConnectionString = "server=localhost;integrated security=SSPI;database=newspaper";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = myConnection;
myConnection.Open();
sqlCommand.CommandText = @"select users.uNO,upwd from users where uNO= @uNo and upwd = @upwd ";
sqlCommand.Parameters.AddWithValue("@uNO",this.textBox1.Text.Trim());
sqlCommand.Parameters.AddWithValue("@upwd",this.textBox2.Text.Trim());
//sqlCommand.CommandType = CommandType.Text;
using(var reader = sqlCommand.ExecuteReader())
{
if (reader.HasRows)
{
this.Hide();
users user = new users ();
user.Show();
}
}
}
你出错的原因是
sqlCommand.CommandText = "select users.uNO,upwd from users where uNO.='" + this.textBox1.Text.Trim() + "'and upwd='" + this.textBox2.Text.Trim() + "'";
中的“uNO.=”多了一个"."。
如果你只是对编程有兴趣或者只是应付课程,接下来的话你就不要看了:
你的控件名没有实际意义,如果是十分庞大的程序,你的代码不具可读性。
你在显示层直接修改数据库,没有使用分层思想,代码耦合性太高,不利于扩展和维护。
你没有使用Sql参数,而是直接拼接Sql语句,这样无法应对Sql攻击,代码健壮性太弱。
实现了IDespose接口的类要用using语句来声明,否则会影响程序性能。
你的reader根本没有定义,怎么可以使用?
reader.Read()虽然返回值是bool类型,但是同时该方法会返回当前游标所在行的数据至reader,如果你只想知道reader里面是否有值,请直接使用reader.HasRows
连接字符串应该放入配置文件,否则一旦离开本机将很难部署
总之,你可以对比一下我的修改(手写,不保证可以正常运行),然后上网查阅一些资料,希望你在C#的学习中顺利。
展开全部
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "server=localhost;integrated security=SSPI;database=newspaper";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = myConnection;
myConnection.Open();
sqlCommand.CommandText = "select users.uNO,upwd from users where uNO.=\'" + this.textBox1.Text.Trim() + "\'and upwd=\'" + this.textBox2.Text.Trim() + "\'";
sqlCommand.CommandType = CommandType.Text;
if (reader.Read())
{
this.Hide();
users user = new users ();
user.Show();
}
--目测是这个转义字符的问题,你可以试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询