用ADO.NET访问数据库的问题
背景:开发带用户登陆程序,登陆后修改密码功能,数据库中有1条用户记录
用户名: admin 密码:123456
问题:
当我用
SELECT ass_user_psw FROM tab_soft_user WHERE ass_user_name='admin'
查询获取密码列表匹配旧密码时,得到的表是空的,请高手帮忙,谢谢先
问题代码如下:
string sCmdLine = "SELECT ass_user_psw FROM tab_soft_user WHERE ass_user_name='" + m_sUser + "'";
SqlDataAdapter thisAdapter = new SqlDataAdapter(sCmdLine, cnt);
DataSet dataset = new DataSet();
thisAdapter.Fill(dataset, "ass_user_psw");
DataRow theRow = dataset.Tables["ass_user_psw"].Rows[0];//这里提示索引0没有可读的数据,另:我用forearch遍例也是空的
if ( sOldPsw.Equals(theRow["ass_user_psw"]) )
{//原密码正确
//判断新密码是否正确
if (sNewPsw.Equals(sNewPswConfirm))
{//正确
//写入数据库新密码
}
else
{//错误
MessageBox.Show("新密码与重复确认密码不同,请重新输入", "错误");
ClearAllTXBControl();
}
}
else
{//原密码错误
MessageBox.Show("原密码错误,请输入正确的原密码", "错误");
ClearAllTXBControl();
} --------------------编程问答-------------------- 为什么不用Reader --------------------编程问答-------------------- 那就是你原来的密码就是空的.所以读到的也是空的
用reader吧 --------------------编程问答-------------------- thisAdapter.Fill(dataset, "ass_user_psw");
改为:
thisAdapter.Fill(dataset, "tab_soft_user");
是表,不是字段
--------------------编程问答-------------------- 除了你本来的数据不对以外
更大的可能是你指定的字段数据类型不对
比如定长的字符型 "ABCD "和"ABCD"是不一样的 等不出来
--------------------编程问答-------------------- 用Reader也一样得到的是什么内容也没有
SQL语句我用 SQL查询分析器 执行可以得到密码是123456,可是我这里却得不到,高人继续指点,谢谢 --------------------编程问答-------------------- 跟踪调试下.
把程序里的SQL语句放在查询分析器里运行下看看 --------------------编程问答-------------------- 跟踪了 (try,catch)了
只是在
DataRow theRow = dataset.Tables["ass_user_psw"].Rows[0];
提示
在位置 0 处没有任何行。
用查询分析器了,一切正常,可以得到数据
不懂啊不懂,帮帮忙吧兄弟们 --------------------编程问答-------------------- 有可能字段后面带空格或回车符号,在查询语句中加入去回车和空格的函数试试
--------------------编程问答-------------------- public static bool Login(Employee employee)
{
//连接数据库
OleDbConnection odconn = null;
DBConnection dbconn = new DBConnection("OracleConnectionString");
try
{
string sql = "select employeename,employeepwd,employeetype from tr_employee where employeename = '" + employee.EmployeeName + "' and employeepwd = '" + employee.EmployeePwd + "' and employeetype = '" + employee.EmployeeType + "' ";
odconn = dbconn.get_Conn();
odconn.Open();
OleDbCommand oldb = new OleDbCommand(sql, odconn);
OleDbDataReader odr = oldb.ExecuteReader(CommandBehavior.CloseConnection);
if (odr.Read())
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
VnvTrip.Terminal.Common.Error.Report(VnvTrip.Terminal.Common.ExceptionTable.Normal_Error, "用户登录数据库链接错误", ex);
return false;
}
finally
{
odconn.Close();
}
}
获取不到密码就一步步的跟踪调试.看程序中的SQL语句在查询管理器中是否能查询到对应的值.
传入一个对象.判断对象属性里的密码是否与数据库中的密码相等.用的是OleDbDataReader.
补充:.NET技术 , C#