需要有关数据库连接和查询代码的帮助

时间:2010-06-23 12:59:02

标签: c# asp.net sql oracle

在下面的代码中,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();

2 个答案:

答案 0 :(得分:3)

您的数据访问代码通常应如下所示:

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对象,但只有在您完成上一个命令后才能使用它。如果需要,也可以打开两个连接。但是, best 选项是在可能的情况下将相关查询合并到单个sql语句中。

答案 1 :(得分:0)

我甚至不打算如何使用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();