简体   繁体   English

SQL命令返回空值

[英]SQL command returning null for value

I am previously only familiar with Linq and the like for data access. 我以前只熟悉Linq等用于数据访问的内容。 I am working on something now that requires me to use actual SQL commands on the back end to return a single value. 我现在正在做一些事情,要求我在后端使用实际的SQL命令来返回单个值。 My code compiles and runs, however it is returning null for a value that I know should be returning something besides an empty string... 我的代码编译并运行,但是对于我知道应该返回除空字符串之外的值的值,它返回null。

Is my structure off on this? 我的构想不对吗? Or is something else missing? 还是缺少其他东西?

Below is my code: 下面是我的代码:

  internal string GetSexDescription(string sex, int id_merchant)
    {
        string newSex = "";

        var builder = new ConnectionStringHelper();
        var connString = builder.getCasinoDBString(id_merchant);

        using (SqlConnection conn = new SqlConnection(connString))
        {
            string sql = "SELECT Description FROM person_gender_lookup WHERE ID = @sex";

            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = sex;

                newSex = cmd.ExecuteScalar().ToString();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return newSex;
        }
    }

Here is a picture of the result set of the table: 这是表的结果集的图片:

在此处输入图片说明

Open the connection. 打开连接。

internal string GetSexDescription(string sex, int id_merchant)
{
   string newSex = "";

   var builder = new ConnectionStringHelper();
   var connString = builder.getCasinoDBString(id_merchant);

   using (SqlConnection conn = new SqlConnection(connString))
   {
      conn.Open(); //<- This line here.
      string sql = "SELECT Description FROM person_gender_lookup WHERE ID = @sex";

      SqlCommand cmd = new SqlCommand(sql, conn);
      try
      {
         cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = sex;

         newSex = cmd.ExecuteScalar().ToString();
      }
      catch(Exception ex)
      {
         Console.WriteLine(ex.Message);
      }

      return newSex;
   }
}

cmd.ExecuteScalar() is probably throwing an InvalidOperationException because you haven't opened the connection. cmd.ExecuteScalar()可能会引发InvalidOperationException因为您尚未打开连接。 The exception is being caught, outputted to the console, then the initial value of newSex is begin returned since the call to ExecuteScalar threw. 捕获到异常后,将其输出到控制台,然后由于对ExecuteScalar的调用被抛出, newSex开始返回newSex的初始值。

ID is a int or varchar? ID是int还是varchar? If is int use: 如果是int使用:

cmd.Parameters.Add("@sex", SqlDbType.Int).Value = sex;

instead of: 代替:

cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = sex;

PS Query parameters and parameter add into cmd.Parameters is case sensitive. PS查询参数和添加到cmd中的参数。参数区分大小写。

Write

@sex

instead of 代替

@Sex

Figured it out. 弄清楚了。 Had to open the cmd and close it AFTER I set the newSex variable to the value being pulled. 在我将newSex变量设置为要拉取的值之后,必须先打开cmd并关闭它。

   internal string GetSexDescription(string sex, int id_merchant)
    {
        string newSex = "";

        var builder = new ConnectionStringHelper();
        var connString = builder.getCasinoDBString(id_merchant);

        DataSet ds = new DataSet();

        using (SqlDataAdapter adapter = new SqlDataAdapter())
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                string sql = "SELECT Description FROM person_gender_lookup WHERE ID = @Sex";

                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();
                    cmd.Connection = conn;
                    adapter.SelectCommand = cmd;
                    cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = sex;
                    adapter.Fill(ds);

                    newSex = cmd.ExecuteScalar().ToString();

                    conn.Close();

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                return newSex;
            }
        }

    }

Try this: 尝试这个:

internal string GetSexDescription(string sex, int id_merchant)
{
    string newSex = "";

    var builder = new ConnectionStringHelper();
    var connString = builder.getCasinoDBString(id_merchant);

    using (SqlConnection conn = new SqlConnection(connString))
    {
        string sql = "SELECT Description FROM person_gender_lookup WHERE ID" +  sex;;

        SqlCommand cmd = new SqlCommand(sql, conn);
        try
        {


            newSex = cmd.ExecuteScalar().ToString();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return newSex;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM