C#winform程序连接Access数据库并实现登录功能,我的代码不对,请问正确的怎么写?

try{stringstrConnection="Provider=Microsoft.Jet.OleDb.4.0;";strConnection+=@"DataSour... try
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";

strConnection += @"Data SourceC:\Users\pc533\Desktop\1.mdb"; // 定义连接

OleDbConnection objConnection = new OleDbConnection(strConnection);

objConnection.Open();

string sql = "select * from user where username='" + this.textBox1.Text + "' and password='" + this.textBox2.Text + "'";

OleDbCommand cmd = new OleDbCommand(sql, objConnection);
int state = Convert.ToInt32(cmd.ExecuteScalar());//执行sql语句,并返回获得值
if (state == 0 || state > 1)//如果数据中没有记录或有多条记录则抱错
{
this.label3.Text = "用户不存在,请检测用户名和密码是否正确!";
}
else
{
this.label3.Text = "登入成功!";

}
objConnection.Close();

}

catch (Exception es)
{
MessageBox.Show("登录失败!");

}

}
展开
 我来答
落月Prc
2011-08-17 · TA获得超过2789个赞
知道大有可为答主
回答量:2011
采纳率:100%
帮助的人:2401万
展开全部
两处错误,第一处改为:
strConnection += @"Data Source=C:\Users\pc533\Desktop\1.mdb"; // 定义连接
第二处改为:
string sql = "select count(*) from user where username='" + this.textBox1.Text + "' and password='" + this.textBox2.Text + "'";
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
庚安阳6q
2011-08-17 · TA获得超过278个赞
知道答主
回答量:65
采纳率:0%
帮助的人:70.8万
展开全部
strConnection += @"Data SourceC:\Users\pc533\Desktop\1.mdb"; // 定义连接
不知道是不是掉了个等号。
strConnection += @"Data Source=C:\Users\pc533\Desktop\1.mdb"; // 定义连接
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
平梦醉神风1I
2011-08-17 · TA获得超过1510个赞
知道小有建树答主
回答量:691
采纳率:0%
帮助的人:1019万
展开全部
1.
strConnection += @"Data Source=C:\Users\pc533\Desktop\1.mdb"; // 定义连接

2.
user和password是acccess的关键字,如下
string sql = string.Format("select count(*) from [user] where username='{0}' and [password]='{1}'",this.textBox1.Text,this.textBox2.Text);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ThinkIBM
2011-08-17 · TA获得超过5289个赞
知道大有可为答主
回答量:3263
采纳率:0%
帮助的人:5418万
展开全部
strConnection += @"Data Source=C:\Users\pc533\Desktop\1.mdb"; // 定义连接

Data Source后面少了个等号
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式