簡體   English   中英

獲取數據集ds.Tables [0] .Rows.Count = 0

[英]Getting DataSet ds.Tables[0].Rows.Count=0

嗨,我正在VS 2010 C#和MySql中開發應用程序

如果憑據存在或不存在,我有一個函數可以驗證。 代碼如下。

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;
    }

在這里,已經有注冊學生的匹配結果,但是我仍然得到count = 0。 不知道怎么做

提出您的建議。

首先,您可以使用sql-injection,使用sql-parameters代替字符串連接。

根據您的實際問題,我必須承認我不確定是什么原因導致了問題。 但是使用具有正確類型的參數通常可以解決以下問題:

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;
    }
}

我有同樣的問題,更多信息,sql數據庫返回0行,但da.Tables(0).Rows.Count = 1。

我的問題是在進行此檢查之前,我在另一個SQL查詢中使用了數據集。 請記住,在執行新的SQL查詢之前,請務必重置數據集,否則您將遇到問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM