我想用VS2010 winform连接ACCESS做个登陆界面.代码老出错
我上个礼拜才接触VS,不要说看书啥的..就要速成交差...拜托各路大神了!我想实现该窗体连接到ACCESS之后,能验证账号密码,正确之后转至主窗体.数据表叫Access,...
我上个礼拜才接触VS,不要说看书啥的..就要速成交差...拜托各路大神了!我想实现该窗体连接到ACCESS之后,能验证账号密码,正确之后转至主窗体.数据表叫Access,两个量是UId 和 UPwd(接触不多所以代码我自己看着都觉得写得跟屎一样...各位别见怪...)
private void btn_Login_Click(object sender, EventArgs e)
{
// OleDbCommand cmd;
//access数据库连接语句
string oleDBString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = F:\visual studio 2010\\Projects\\Graduation Project\\g_pData.mdb";
//创建连接对象
OleDbConnection conn = new OleDbConnection(oleDBString);
//打开数据库
conn.Open();
if ((txt_Login.Text != "") && (txt_LoginPwd.Text != ""))
{
string sql = "SELECT Count(*) FROM Account WHERE id='" + txt_Login.Text + "'And pwd = '" + txt_LoginPwd.Text + "'";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
if (dr.GetInt32(0) > 0)
{
MessageBox.Show("OK");
this.Hide();
MainForm main = new MainForm();
main.Show();
}
else
{
MessageBox.Show("Wrong");
}
}
else
{ MessageBox.Show("None!"); }
dr.Close();
}
} 展开
private void btn_Login_Click(object sender, EventArgs e)
{
// OleDbCommand cmd;
//access数据库连接语句
string oleDBString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = F:\visual studio 2010\\Projects\\Graduation Project\\g_pData.mdb";
//创建连接对象
OleDbConnection conn = new OleDbConnection(oleDBString);
//打开数据库
conn.Open();
if ((txt_Login.Text != "") && (txt_LoginPwd.Text != ""))
{
string sql = "SELECT Count(*) FROM Account WHERE id='" + txt_Login.Text + "'And pwd = '" + txt_LoginPwd.Text + "'";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
if (dr.GetInt32(0) > 0)
{
MessageBox.Show("OK");
this.Hide();
MainForm main = new MainForm();
main.Show();
}
else
{
MessageBox.Show("Wrong");
}
}
else
{ MessageBox.Show("None!"); }
dr.Close();
}
} 展开
展开全部
private void btn_Login_Click(object sender, EventArgs e)
{
// OleDbCommand cmd;
//access数据库连接语句。注意!字符串前用了@后,字符串中只需单个\
string oleDBString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = F:\visual studio 2010\Projects\Graduation Project\g_pData.mdb";
//创建连接对象
OleDbConnection conn = new OleDbConnection(oleDBString);
//打开数据库
conn.Open();
if ((txt_Login.Text != "") && (txt_LoginPwd.Text != ""))
{
string sql = "SELECT Count(*) FROM Account WHERE id='" + txt_Login.Text + "'And pwd = '" + txt_LoginPwd.Text + "'";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
if (dr.GetInt32(0) > 0)
{
MessageBox.Show("OK");
this.Hide();
MainForm main = new MainForm();
main.Show();
//关闭登陆窗口
this.Close();
}
else
{
MessageBox.Show("Wrong");
}
}
else
{
MessageBox.Show("None!");
}
dr.Close();
}
}
更多追问追答
追答
数据库中的字段名分别为:UId和UPwd,不是id和pwd
string sql = "SELECT Count(*) FROM Account WHERE UId='" + txt_Login.Text + "'And UPwd = '" + txt_LoginPwd.Text + "'";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询