簡體   English   中英

在數據庫連接和查詢代碼方面需要幫助

[英]Need help with database connection and query code

在下面的代碼中,cmdquery有效,而hrquery無效。 如何獲取另一個查詢以填充網格視圖? 我需要建立一個新的連接還是使用相同的連接? 你們能幫我嗎? 我是C#和ASP的新手。 這是我整理的一些意大利面代碼。 這可能都是錯誤的,因此,如果您有更好的方法來共享此內容,請隨意分享。

if (Badge != String.Empty)
{
    string cmdquery = "SELECT * from Employees WHERE Badge ='" + Badge + "'";
    string hrquery = "SELECT CLOCK_IN_TIME, CLOCK_OUT_TIME FROM CLOCK_HISTORY   WHERE Badge ='" + Badge + "'";

    OracleCommand cmd = new OracleCommand(cmdquery);
    cmd.Connection = conn;
    cmd.CommandType = CommandType.Text;
    conn.Open();

    OracleDataReader reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        this.xUserNameLabel.Text += reader["EMPLOYEE_NAME"];
        this.xDepartmentLabel.Text += reader["REPORT_DEPARTMENT"];               
    }

    OracleCommand Hr = new OracleCommand(hrquery);
    Hr.Connection = conn;
    Hr.CommandType = CommandType.Text;

    OracleDataReader read = Hr.ExecuteReader();

    while (read.Read())
    {
        xHoursGridView.DataSource = hrquery;
        xHoursGridView.DataBind();
    }
}
conn.Close();

您的數據訪問代碼通常應如下所示:

string sql = "SELECT * FROM Employee e INNER JOIN Clock_History c ON c.Badge = e.Badge WHERE e.Badge = @BadgeID";
using (var cn = new OracleConnection("your connection string here"))
using (var cmd = new OracleCommand(sql, cn))
{
    cmd.Parameters.Add("@BadgeID", OracleDbType.Int).Value = Badge;

    cn.Open();

    xHoursGridView.DataSource = cmd.ExecuteReader();
    xHoursGridView.DataBind();
}

請注意,這只是常規模板。 您將需要針對您的確切需求對其進行調整。 從中獲得的重要信息是using塊,用於正確創建和布置您的連接對象以及防止SQL注入的參數。

關於連接問題,也有例外,但是通常一次只能將一個連接用於一個活動結果集。 因此,您可以重用原始代碼中的同一個conn對象,但前提是您必須完全使用上一個命令完成它。 如果需要,可以打開兩個連接。 但是, 最好的選擇是在可能的情況下將相關查詢合並到單個sql語句中。

我什至不打算using s和方法來using你的方法:p

if (Badge != String.Empty)
    {

        string cmdquery = "SELECT * from Employees WHERE Badge ='" + Badge + "'";
        string hrquery = "SELECT CLOCK_IN_TIME, CLOCK_OUT_TIME FROM CLOCK_HISTORY   WHERE Badge ='" + Badge + "'";

        OracleCommand cmd = new OracleCommand(cmdquery);
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        conn.Open();

        OracleDataReader reader = cmd.ExecuteReader();


            while (reader.Read())
            {
                this.xUserNameLabel.Text += reader["EMPLOYEE_NAME"];
                this.xDepartmentLabel.Text += reader["REPORT_DEPARTMENT"];

            }


            OracleCommand Hr = new OracleCommand(hrquery);
            Hr.Connection = conn;
            Hr.CommandType = CommandType.Text;


            OracleDataReader read = Hr.ExecuteReader();

            //What's this next line? Setting the datasource automatically
            // moves through the data.
            //while (read.Read())
            //{
                                          //I changed this to "read", which is the
                                          //datareader you just created.
                xHoursGridView.DataSource = read;
                xHoursGridView.DataBind();
            //}


    }
    conn.Close();

暫無
暫無

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

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