[英]Getting DataSet ds.Tables[0].Rows.Count=0
Hi I am developing an application in VS 2010 C# and MySql 嗨,我正在VS 2010 C#和MySql中开发应用程序
I have a function to validate is provided credentials are exist or not. 如果凭据存在或不存在,我有一个函数可以验证。 Code is as below.
代码如下。
private bool Validate(string studentId, string time)
{
var msCon = _dal.OpenMySQLConnection();
var da = new MySqlDataAdapter("SELECT * FROM tblStudent where Registeration_Date='" + time.Trim() + "' and studentId='" + studentId.Trim() + "' order by date_time desc limit 1", msCon);
var ds = new DataSet();
da.Fill(ds);
var count = ds.Tables[0].Rows.Count;
if (count == 0)
{
updateOrExist.Text = @"E";
updationTime.Text = @"Current Checked Time: " + DateTime.Now.ToShortTimeString();
return true;
}
updationTime.Text = @"Current updated Time: " + DateTime.Now.ToShortTimeString();
return false;
}
In this there is already registered student with matching result but still I am getting count=0. 在这里,已经有注册学生的匹配结果,但是我仍然得到count = 0。 Don't know how.
不知道怎么做
Requeting your suggestions. 提出您的建议。
First, you are open for sql-injection, use sql-parameters instead of string concatenation. 首先,您可以使用sql-injection,使用sql-parameters代替字符串连接。
According to your actual issue, i must admit that i'm not sure what causes the problem. 根据您的实际问题,我必须承认我不确定是什么原因导致了问题。 But using parameters with the correct types often solves such issues:
但是使用具有正确类型的参数通常可以解决以下问题:
private bool Validate(string studentId, string time)
{
DateTime registrationDate;
if (!DateTime.TryParse(time.Trim(), out registrationDate))
throw new ArgumentException("Time must be convertible to datetime", "time");
int id;
if (!int.TryParse(studentId.Trim(), out id))
throw new ArgumentException("StudentId must be convertible to Integer", "studentId");
string sql = @"
SELECT * FROM tblStudent
WHERE Registeration_Date=?Registeration_Date
AND StudentID=?StudentID
ORDER BY date_time desc
limit 1";
using (var msCon = new MySqlConnection("connection-string"))
using (var da = new MySqlDataAdapter(sql, msCon))
{
da.SelectCommand.Parameters.AddWithValue("?Registeration_Date", registrationDate);
da.SelectCommand.Parameters.AddWithValue("?StudentID", id);
var table = new DataTable();
da.Fill(table);
return table.Rows.Count = 1;
}
}
I had the same problem, more info, the sql database returns 0 row(s) but da.Tables(0).Rows.Count=1. 我有同样的问题,更多信息,sql数据库返回0行,但da.Tables(0).Rows.Count = 1。
My problem was that before this check, I used the dataset in another SQL query. 我的问题是在进行此检查之前,我在另一个SQL查询中使用了数据集。 REMEMBER ALWAYS TO RESET THE DATASET BEFORE NEW SQL QUERY ANSWER, or you will have problems.
请记住,在执行新的SQL查询之前,请务必重置数据集,否则您将遇到问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.