SQL检查密码是对还是错
问题描述:
我有一个SQL CE数据库.密码必须由用户提供.问题是当我连接到数据库时,找不到任何方法可以检查给定的密码是否正确.
Hi,
I have a SQL CE database. the password have to be given by user. The problem is when I connect to database, I find no way to check if the given password is corrected or not.
public partial class UsersDB : DataContext
{
public Table<Users.UserLoginData> UsersTable = null;
public UsersDB(string connection, string password): base(connection)
{
if (!DatabaseExists())
CreateDatabase();
bool endLoop = false;
while(!endLoop)
{
if (password != "")
{
try
{
base.Connection.ConnectionString ="datasource="+ connection
+ "; Password=" + password;
}
catch(Exception ex)
{
MessageBox.Show("Wrong connection type");
}
....
尝试访问数据之前,错误的密码不会引发异常.另外,我不确定以前的方式是否是业余爱好者:
Wrong password does not throw an exception until a try to accessing data happens. Also I''m not sure if my previous way was an amateur or not:
try
{
UsersTable.Count();
endLoop = true;
}
catch(System.Data.SqlServerCe.SqlCeException ex)
{
if (ex.Message.ToLower().IndexOf("password") >= 0)
{
....
如果我想将类更改为基类,则没有有关派生类中的表的信息.
作为基类,不能使用像此处的"UsersTable"之类的表.
有人可以帮我吗?
If I want to change the class to a base class I have no information about tables in derived classes.
As a base class it is not possible to use Tables such as "UsersTable" like here.
Can anyone help me?
答
查找密码是否不正确的唯一方法是执行一个简单的查询....类似选择顶部1 1来自sys.objects''或类似的东西...并添加错误处理以捕获不正确的密码错误
制作一个函数(checkConnection),该函数执行一些简单的查询并根据是否捕获到错误的登录错误分别返回true或false.
The only way to find if the password is incorrect is by executing a simple query....something like ''select top 1 1 from sys.objects'' or something like that...and adding error handling to catch a incorrect password error
Make a function (checkConnection) which executes some such simple query and returns true or false depending on whether the incorrect login error is caught or not respectively.