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("登录失败!");
}
} 展开
{
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("登录失败!");
}
} 展开
展开全部
两处错误,第一处改为:
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 + "'";
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 + "'";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
strConnection += @"Data SourceC:\Users\pc533\Desktop\1.mdb"; // 定义连接
不知道是不是掉了个等号。
strConnection += @"Data Source=C:\Users\pc533\Desktop\1.mdb"; // 定义连接
不知道是不是掉了个等号。
strConnection += @"Data Source=C:\Users\pc533\Desktop\1.mdb"; // 定义连接
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
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);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
strConnection += @"Data Source=C:\Users\pc533\Desktop\1.mdb"; // 定义连接
Data Source后面少了个等号
Data Source后面少了个等号
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询