【问题标题】:C# Asp.net Webforms simple SQL Query for 1 row several columns to variablesC# Asp.net Webforms 简单 SQL 查询 1 行多列变量
【发布时间】:2020-07-15 22:58:58
【问题描述】:

我一直在四处寻找,要么让自己感到困惑,要么没有找到正确的东西。

我有这个数据阅读器,它可以为存储过程提取一些信息。但我认为我做得不对。

string constr = ConfigurationManager.ConnectionStrings["PAYROLL"].ConnectionString;

using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("DLI_EMPLOYEE_PORTAL_EMPLOYEE_INFORMATION"))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EID", Session["sessionEMPID"].ToString());
        cmd.Connection = con;

        con.Open();
        SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        while (dataReader.Read())
        {
            string EMP_FIRST = dataReader["FIRST_NAME"].ToString();
            string EMP_LAST = dataReader["LAST_NAME"].ToString();
            string EMP_DEPT = dataReader["DEPT"].ToString();
            string EMP_DEPT_ID = dataReader["DEPT_ID"].ToString();

            body = body + "<p>SUBMITTED BY : (" + Session["sessionEMPID"].ToString() + ") " + EMP_FIRST + " " + EMP_LAST + " - DEPT : " + EMP_DEPT + "</p> " + System.Environment.NewLine;
        }

        con.Close();
    }
}

我只需要根据员工 ID 查询一行。我宁愿不通过存储过程,而是通过选择查询。

SELECT e.FIRST_NAME, e.LAST_NAME, e.DEPT_ID, d.NAME 
FROM EMPLOYEE AS e 
INNER JOIN DEPARTMENT AS d ON e.DEPT_ID = d.ID 
WHERE (e.ID = 'sim01')

我正在构建一个 HTML 正文字符串,这就是我需要这些信息的原因。

body = body + "<p>SUBMITTED BY : (" + Session["sessionEMPID"].ToString() + ") " + EMP_FIRST + " " + EMP_LAST + " - DEPT : " + EMP_DEPT + "</p> " + System.Environment.NewLine;

非常感谢任何帮助。谢谢。

【问题讨论】:

  • 您的代码看起来不错,但您应该阅读Can we stop using AddWithValue() already?
  • 所以你想使用选择语句而不是存储过程。伟大的。是什么阻止你这样做?你试过了吗?您的实施出了什么问题?
  • 数据读取器不是在循环吗?我想我正在寻找我喜欢的经典 ASP。
  • strSQLQuery = "...." Set MyRS = Server.CreateObject("ADODB.RecordSet") Set MyRS = dli.Execute(strSQLQuery) 'RESPONSE.WRITE "

    last姓名:" & (MyRS("LAST_NAME"))

  • 你有一个while循环,所以从技术上讲你有一个循环。如果只返回一行,那么它将通过循环一次。我建议您查看Dapper,它具有更好的语法来使用 ADO.NET。它可以让你做类似var information = con.Query("your select command", new { EmployeeId = Session["sessionEMPID"].ToString()} ).SingleOrDefault());

标签: c# asp.net sql-server tsql webforms


【解决方案1】:

如果您只想使用查询而不是存储过程,只需将您的 SQL 语句传递给 Command 并将您的 CommandType 设置为 Text。如果您只希望有一行,请使用 if (dataReader.Read() 而不是 while (dataReader.Read())

string constr = ConfigurationManager.ConnectionStrings["PAYROLL"].ConnectionString;

using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand(
        "SELECT e.FIRST_NAME, e.LAST_NAME, e.DEPT_ID, d.NAME " + 
        "FROM EMPLOYEE AS e " +
        "INNER JOIN DEPARTMENT AS d ON e.DEPT_ID = d.ID " + 
        "WHERE (e.ID = @EID)"));
    {
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@EID", Session["sessionEMPID"].ToString());
        cmd.Connection = con;

        con.Open();
        SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        if (dataReader.Read())
        {
            string EMP_FIRST = dataReader["FIRST_NAME"].ToString();
            string EMP_LAST = dataReader["LAST_NAME"].ToString();
            string EMP_DEPT = dataReader["DEPT"].ToString();
            string EMP_DEPT_ID = dataReader["DEPT_ID"].ToString();

            body = body + "<p>SUBMITTED BY : (" + Session["sessionEMPID"].ToString() + ") " + EMP_FIRST + " " + EMP_LAST + " - DEPT : " + EMP_DEPT + "</p> " + System.Environment.NewLine;
        }

        con.Close();
    }
}

如果查询可以返回多行,您可以将TOP 1 添加到带有ORDER BY &lt;some other field&gt; 的查询中以仅获取最相关的行。

【讨论】:

    【解决方案2】:

    如果没有 TSQL 逻辑,我们最好使用查询而不是存储过程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多