问题描述
- 老是抛出connection没被初始化,怎么破
-
private void FrmSysLogin_Load(object sender, EventArgs e)
{
conStr = @"Data Source=2011-20131004OT;Initial Catalog=Bank;Integrated Security=true";} private void btnLogin_Click(object sender, EventArgs e) { SqlConnection connection = new SqlConnection(conStr); SqlCommand cmd = new SqlCommand(); connection .Open ( ); cmd .CommandText = " SELECT*FROM Bank WHERE cardID@='6301'&&<='6310' AND pwd=='888888'"; SqlParameter paraID = new SqlParameter(); paraID.ParameterName = "cardID"; paraID.DbType = System.Data.DbType.String; paraID.SqlDbType = System.Data.SqlDbType.Char; paraID.Direction = System.Data.ParameterDirection.InputOutput; paraID.IsNullable = true; paraID.Value = "10000"; cmd.Parameters.Add(paraID); SqlParameter paraPWD = new SqlParameter(); paraPWD.ParameterName = "pwd"; paraPWD.DbType = System.Data.DbType.String; paraPWD.SqlDbType = System.Data.SqlDbType.Char; paraPWD.Direction = System.Data.ParameterDirection.InputOutput; paraPWD.IsNullable = true; paraPWD.Value = "888888"; cmd.Parameters.Add(paraPWD); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { ; MessageBox.Show("登录成功!"); } else { MessageBox.Show("登录失败!"); } connection.Close();
解决方案
代码很多错误,sql也不对,sql中哪里能用&&运算符呢?你几乎一窍不通。
解决方案二:
SqlDataReader reader = cmd.ExecuteReader();
就是上面这条语句老是说CONNECTION没有被初始化。。。。怎么办
快来人哇~~~~求打救
解决方案三:
SqlDataReader reader = cmd.ExecuteReader();
就是上面这条语句老是说CONNECTION没有被初始化。。。。怎么办
快来人哇~~~~求打救
解决方案四:
SqlCommand cmd = conn.CreateCommand();
或者
SqlCommand cmd = new SQLCommand(sql语句,conn);
解决方案五:
cmd .CommandText = " SELECT*FROM Bank WHERE cardID@='6301'&&<='6310' AND pwd=='888888'";
改为
cmd .CommandText = " SELECT*FROM Bank WHERE cardID>='6301' and cardID <='6310' AND pwd='888888'";
既可。。
解决方案六:
先在方法外对connection置空值,然后在在方法里new
解决方案七:
这种写法真心没见过。或许你可以试试这个
private const string conn = "Data Source=服务器名; Initial Catalog=数据库名; User id=用户名; Pwd=密码";
private SqlConnection connection;
private SqlCommand command;
private SqlDataReader reader;
/**
*打开连接
*/
private void getCon()
{
try
{
connection = new SqlConnection(conn);
connection.Open();
}
catch (Exception)
{
throw;
}
}
/**
*关闭连接
*/
private void closeCon()
{
if (reader != null)
{
reader.Close();
}
if (connection != null)
{
connection.Close();
}
}
/**
*增删改方法分装
*/
public int IDU(string sql,SqlParameter [] para)
{
this.OpenCon();
command = new SqlCommand(sql, this.connection);
command.Parameters.AddRange(para);
int flg = command.ExecuteNonQuery();
this.CloseCon();
return flg;
}
/**
*聚合函数方法封装,常用于登录验证
*/
public int doLogin(string sql,SqlParameter [] para)
{
this.OpenCon();
command = new SqlCommand(sql, this.connection);
command.Parameters.AddRange(para);
int flg = (int)command.ExecuteScalar();
this.CloseCon();
return flg;
}
/**
*查询方法
*/
public SqlDataReader GetRead(string sql,SqlParameter [] para)
{
this.OpenCon();
command = new SqlCommand(sql, this.connection);
command.Parameters.AddRange(para);
reader = command.ExecuteReader();
return reader;
}
以上是VS常用的方法封装,
如下是调用
*查询 数据并绑定到ListViewItem上
*/
string sql = "select * from Admin";
SqlParameter [] para={};
SqlDataReader reader = this.GetRead(sql,para);
while(reader.Read()){
string no = reader["LoginId"].ToString();
string pwd = reader["LoginPwd"].ToString();
ListViewItem lvi = new ListViewItem();
lvi = new ListViewItem(no);
lvi.SubItems.Add(pwd);
this.listView1.Items.Add(lvi);
}
/**
*登录验证
*/
string sql="select count(*) from userinfo where userPass=@userPass and userName=@userName";
SqlParameter[] para =
{
new SqlParameter("@userPass","123"),
new SqlParameter("@userName","张三")
};
int flg=this.doLogin(sql,para);
//flg>0有该用户,flg<=0无该用户
/**
*新增,修改,删除,此处只举例新增
*/
string sql="insert into userinfo values(@userName,@userPass)";
SqlParameter[] para =
{
new SqlParameter("@userName","张三"),
new SqlParameter("@userPass","123")
};
int flg=this.IDU(sql,para);
//flg>0 新增成功 flg<0新增失败
或许你可以研究研究
解决方案八:
这种写法真心没见过。或许你可以试试这个
private const string conn = "Data Source=服务器名; Initial Catalog=数据库名; User id=用户名; Pwd=密码";
private SqlConnection connection;
private SqlCommand command;
private SqlDataReader reader;
/**
*打开连接
*/
private void getCon()
{
try
{
connection = new SqlConnection(conn);
connection.Open();
}
catch (Exception)
{
throw;
}
}
/**
*关闭连接
*/
private void closeCon()
{
if (reader != null)
{
reader.Close();
}
if (connection != null)
{
connection.Close();
}
}
/**
*增删改方法分装
*/
public int IDU(string sql,SqlParameter [] para)
{
this.OpenCon();
command = new SqlCommand(sql, this.connection);
command.Parameters.AddRange(para);
int flg = command.ExecuteNonQuery();
this.CloseCon();
return flg;
}
/**
*聚合函数方法封装,常用于登录验证
*/
public int doLogin(string sql,SqlParameter [] para)
{
this.OpenCon();
command = new SqlCommand(sql, this.connection);
command.Parameters.AddRange(para);
int flg = (int)command.ExecuteScalar();
this.CloseCon();
return flg;
}
/**
*查询方法
*/
public SqlDataReader GetRead(string sql,SqlParameter [] para)
{
this.OpenCon();
command = new SqlCommand(sql, this.connection);
command.Parameters.AddRange(para);
reader = command.ExecuteReader();
return reader;
}
以上是VS常用的方法封装,
如下是调用
*查询 数据并绑定到ListViewItem上
*/
string sql = "select * from Admin";
SqlParameter [] para={};
SqlDataReader reader = this.GetRead(sql,para);
while(reader.Read()){
string no = reader["LoginId"].ToString();
string pwd = reader["LoginPwd"].ToString();
ListViewItem lvi = new ListViewItem();
lvi = new ListViewItem(no);
lvi.SubItems.Add(pwd);
this.listView1.Items.Add(lvi);
}
/**
*登录验证
*/
string sql="select count(*) from userinfo where userPass=@userPass and userName=@userName";
SqlParameter[] para =
{
new SqlParameter("@userPass","123"),
new SqlParameter("@userName","张三")
};
int flg=this.doLogin(sql,para);
//flg>0有该用户,flg<=0无该用户
/**
*新增,修改,删除,此处只举例新增
*/
string sql="insert into userinfo values(@userName,@userPass)";
SqlParameter[] para =
{
new SqlParameter("@userName","张三"),
new SqlParameter("@userPass","123")
};
int flg=this.IDU(sql,para);
//flg>0 新增成功 flg<0新增失败
或许你可以研究研究
时间: 2024-10-03 22:01:29