【问题标题】:Using variables vs SQL Server in using statement在 using 语句中使用变量与 SQL Server
【发布时间】:2017-08-29 10:55:40
【问题描述】:

有人可以帮助我了解发生了什么吗?我正在尝试获取

  1. SAQA ID
  2. NQF 级别
  3. 学分

当然,但有些东西我不明白。

如果我用我的查询创建一个变量,例如

public Int32 T_Course_Id = 0, T_Company_Id = 0, T_Nqf = 0, T_Credit = 0;

string queryTaskId = "SELECT [course_saqa_id] FROM"+
                     "[sta].[dbo].[Courses]"+
                     "WHERE course_name = '" + _Coursename + "'";

string queryNqf = "SELECT [course_nqf]"+
                  "FROM [sta].[dbo].[Courses]"+
                  "WHERE course_saqa_id = '" + T_Course_Id + "'";

using (SqlConnection Conn = new SqlConnection(ConnString))
{
    Conn.Open();

    using (SqlCommand command = new SqlCommand(queryTaskId, Conn))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            if (reader.HasRows)
            {
                reader.Read();
                // Call Read before accessing data. 
                T_Course_Id = reader.GetInt32(0);
            }

            // Call Close when done reading.
            reader.Close();
        }
    }

    using (SqlCommand command = new SqlCommand(queryCredit, Conn))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            if (reader.HasRows)
            {
                reader.Read();

               // Call Read before accessing data. 
                T_Credit = reader.GetInt32(0);
            }

            // Call Close when done reading.
            reader.Close();
        }
    }                    

    Conn.Close();
}

如果我这样做,我会得到 T_Credit 变量的 0 值,但如果我这样做(这只是最后一部分)

using (SqlCommand command = new SqlCommand("SELECT [course_nqf] FROM [sta].[dbo].[Courses] WHERE course_saqa_id = '" + T_Course_Id + "'", Conn))
{
    using (SqlDataReader reader = command.ExecuteReader())
    {
        if (reader.HasRows)
        {
            reader.Read();
            // Call Read before accessing data. 
            T_Credit = reader.GetInt32(0);
        }

        // Call Close when done reading.
        reader.Close();
    }
 }                    

然后我得到正确的值,你可以看到我直接传递 SQL 命令而不是变量

using (SqlCommand command = new SqlCommand("SELECT [course_nqf] FROM [sta].[dbo].[Courses] WHERE course_saqa_id = '" + T_Course_Id + "'", Conn))

为什么变量在这里不起作用?

【问题讨论】:

  • 您是否尝试过使用模板字符串? C# 6 此外,当你已经在使用 using() { ... } 样式时,为什么还要调用 close ?
  • 看起来这只是一个错字,您在第一种情况下使用了错误的变量。您应该使用queryNqf 而不是queryCredit
  • SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看Little Bobby Tables
  • 如果您像这样将 SQL 语句连接在一起,在关键字之间留一些 空格 也会很有帮助.....例如使用"SELECT [course_saqa_id] FROM " + "[sta].[dbo].[Courses]" + " WHERE course_name = @CourseName"; - 注意FROM 之后和WHERE 关键字之前的SPACE ....

标签: c# sql variables logic


【解决方案1】:

操作顺序很重要。

在您的第一个示例中: 您正在设置T_Course_ID = 0 的值,然后立即创建一个查询字符串。当时,T_Course_ID 值被评估为0 并被遗忘。每次使用字符串连接时都不会对其进行评估。仅在分配时。

在你的第二个例子中: T_Course_ID 被您的初始查找分配了一个新值。由于SQLCommand是在变量重新赋值后执行的,所以每次都使用新值进行计算。

这是一个使用 javascript 的示例,同样适用于那里:

var T_Course_Id = 0;

var queryNqf =
  "SELECT [course_nqf]" +
  "FROM [sta].[dbo].[Courses]" +
  "WHERE course_saqa_id = '" + T_Course_Id + "'";

// Query displays initialized value
console.log(queryNqf);

// Variable is changed
T_Course_Id = 'I Will NOT CHANGE';

// Try to log the updated value but it doesn't work because queryNqf was assigned too early
console.log(queryNqf);

// Reassign with the new value
queryNqf =
  "SELECT [course_nqf]" +
  "FROM [sta].[dbo].[Courses]" +
  "WHERE course_saqa_id = '" + T_Course_Id + "'";

// Retrieve the expect result.
console.log(queryNqf);

【讨论】:

    【解决方案2】:

    您的 using 语句中似乎没有使用正确的命令字符串 queryNqf。

    你的:使用 (SqlCommand command = new SqlCommand(queryCredit, Conn))

    可能是:使用 (SqlCommand command = new SqlCommand(queryNqf , Conn))

    【讨论】:

      猜你喜欢
      • 2011-09-03
      • 2016-11-18
      • 1970-01-01
      • 2013-05-24
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多