这个ACCESS数据库文件名是TelListingManager.mdb,在D盘根目录下和程序的debug文件夹下,就是跟这个exe 文件同目录的也有个,同目录的报错,我试了试D盘根目录也出错,顺便问下同一个目录下代码怎么写,不胜感激!
private void btnLogin_Click(object sender, EventArgs e)
{
//string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + MapPath("TelListingManager.mdb");
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=d:\\TelListingManager.mdb";
OleDbConnection connection = new OleDbConnection(strConnection);
OleDbCommand cmd= new OleDbCommand("select count(id) as c from Users where userName="+txtUserName.Text.ToLower()+" and password="+txtPassword.Text, connection);
//OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
//DataSet ds = new DataSet();
//oda.Fill(ds);
//int count = (int)ds.Tables[0].Rows[0]["c"];
connection.Open();
OleDbDataReader dr = cmd.ExecuteReader();
int count = (int)dr["c"];
if (count >= 1)
{
frmMain _frmMain = new frmMain();
_frmMain.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入!","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtPassword.Text = "";
txtPassword.Focus();
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
//string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + MapPath("TelListingManager.mdb");
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=d:\\TelListingManager.mdb";
OleDbConnection connection = new OleDbConnection(strConnection);
OleDbCommand cmd= new OleDbCommand("select count(id) as c from Users where userName="+txtUserName.Text.ToLower()+" and password="+txtPassword.Text, connection);
//OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
//DataSet ds = new DataSet();
//oda.Fill(ds);
//int count = (int)ds.Tables[0].Rows[0]["c"];
connection.Open();
OleDbDataReader dr = cmd.ExecuteReader();
int count = (int)dr["c"];
if (count >= 1)
{
frmMain _frmMain = new frmMain();
_frmMain.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入!","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtPassword.Text = "";
txtPassword.Focus();
}
}
OleDbDataReader dr = cmd.ExecuteReader();
Data Source=TelListingManager.mdb
就行了
不知道你出现在异常是什么异常...如果你是想取读出来的记录的条数的话,用你注释掉的方法就行了,如果你的SQL没错的话...
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
int count = ds.Tables[0].Rows.Count;
如果手写SQL的错误的话,建议用自带的数据源控件自动连接然后复制生成的SQL,可以避免一些格式错误...
OleDbCommand cmd= new OleDbCommand("select count(id) as c from users where username='+txtUserName.Text.ToLower()+' and password='+txtPassword.Text'", connection); 另外再请教一下,我是菜鸟,现在正在上学,我原来做C#连接SQL SERVER的时候用
int count=(int) ds.Tables[0].Rows[0]["c"]就可以,在这里怎么都查不出来,用您这句真管用
int count=(int) ds.Tables[0].Rows.Count;这两句话有什么区别啊?能简单指点一下吗?
用你的这一行int count=(int) ds.Tables[0].Rows.Count;怎么输都是正确的
用这一行int count=(int) ds.Tables[0].Rows[0]["c"]怎么输都是错的,我的数据库里users表里有三列如下
id username password
1 123 123SQL语句是这个
OleDbCommand cmd= new OleDbCommand("select count(id) as c from users where username='+txtUserName.Text.ToLower()+' and password='+txtPassword.Text'", connection);
到底哪个环节错了啊
ds.Table[0].Rows[0]["c"]是取得第0行的"C"列的值,怎么会得到行数?
int count=(int) ds.Tables[0].Rows.Count 是查询tables[0]所有符合的行的总数
也就是,只要表里面查询的数据条数的总数不等于或小于0,就说明有这个用户。------------------------------------------------------------------------------------------OleDbCommand cmd= new OleDbCommand("select count(id) as c from users where username='+txtUserName.Text.ToLower()+' and password='+txtPassword.Text'", connection);
到底哪个环节错了啊
//------------------------
txtUserName.Text.ToLower()/txtPassword.Text是字符吗?你的用户名不会就一个字符吧,显然是字符串!那你就只用单引号,当然就错了!count(ID)你是要统计数据表数据条数的总和?错!id是自增字段吧,哪就是从1到无穷大,你加起来的数不等于你数据库里的数据总和。所以,你得用2楼的方法,先填充到dataset或者datatable,然后看查到的数据总和是否大于0;如果你一定要用上面的方法的话,哪也得这么写。oledbcommand cmd=new oledbcommand("select id as c from users where username='"+textbox1.text+"'and password='"+textbox2.text+"'";(如果数据库password字段为整数,那就用这个'"+int.parse(textbox2.text)+'") if (count > = 1) 这个改成(if(dr.read())如果读到了数据) if(dr.read())//返回BOOL值TRUE就执行
{
frmMain _frmMain = new frmMain();
_frmMain.Show();
this.Hide();
}
试试改改看,看还有错没!
string cmmd = string.format("select count(id) as c from users where username='{0}' and password='{1}' ",txtUserName.Text.ToLower(),txtPassword.Text );oledbcommand cmd = new oledbcommand(cmmd);OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds); // 这个 就该是你要取得的数量 ds.Table[0].Rows[0]["c"]