变量问题

时间:2019-03-29 13:56:49

标签: sql ssms

我正在使用此代码从数据库获取数据。 问题是 query var ,其中出现错误:

  

System.Exception:'指定的强制转换无效。 System.Data'

选择正常,我正在使用SQL Server Management Studio。

出什么问题了?下面的代码:

@celery.task(base=MyTask, foo="bar")
def add(x, y):
    return x + y

1 个答案:

答案 0 :(得分:0)

尝试将 cast myreader.GetInt32()更改为 convert Convert.ToInt32(...),即

    var ob = new StrukturaObrok(
      // Conversion instead of Cast
      Convert.ToInt32(myreader["Pin"]),

      Convert.ToString(myreader["Name"]),
      Convert.ToString(myreader["Surname"]),
      Convert.ToString(myreader["M1"]),
      Convert.ToString(myreader["M2"]),
      Convert.ToString(myreader["M3"]),
      Convert.ToString(myreader["M4"]),            

      Convert.ToInt32(myreader["M1Cijena"]),
      Convert.ToInt32(myreader["M2Cijena"]),
      Convert.ToInt32(myreader["M3Cijena"]),
      Convert.ToInt32(myreader["M4Cijena"]),  

      Convert.ToString(myreader["UkupnoM1_M2"]),
      Convert.ToString(myreader["UkupnoM3_M4"])
    );

代码:

List<StrukturaObrok> obrok = new List<StrukturaObrok>();

connectionString = string.Join(";", 
  "Data Source=LAPTOP-QVQ8QR1I",
  "Initial Catalog=BoneIDa2",
  "Integrated Security=SSPI");

//DONE: wrap IDisposable into using
using (SqlConnection conn = new SqlConnection(connectionString)) {
  conn.Open();

  //DONE: Keep sql readable
  string sql =  
    @"select u.pin, 
             u.firstname Name, 
             u.lastname Surname, 
             ISNULL(CONVERT(varchar(50), sum(case when tmt.type = 'M1' then tmt.Price end)), '') as M1, 
             ISNULL(CONVERT(varchar(50), sum(case when tmt.type = 'M2' then tmt.Price end)), '') as M2, 
             ISNULL(CONVERT(varchar(50), sum(case when tmt.type = 'M3' then tmt.Price end)), '') as M3, 
             ISNULL(CONVERT(varchar(50), sum(case when tmt.type = 'M4' then tmt.Price end)), '') as M4, 
             (select Price from TA_MealsType Where type = 'M1') M1Cijena, 
             (select Price from TA_MealsType Where type = 'M2') M2Cijena, 
             (select Price from TA_MealsType Where type = 'M3') M3Cijena, 
             (select Price from TA_MealsType Where type = 'M4') M4Cijena, 
             ISNULL(CONVERT(varchar(50), sum(case when tmt.type in ('M1', 'M2') then tmt.Price end)), '') as UkupnoM1_M2, 
             ISNULL(CONVERT(varchar(50), sum(case when tmt.type in ('M3', 'M4') then tmt.Price end)), '') as UkupnoM3_M4 
        from users u 
             left join TA_Meals tm on u.pin = tm.pin 
             left join TA_MealsType tmt on tm.MealType = tmt.id 
       where u.department = 1000001001 
    group by u.pin, u.firstname, u.lastName"; 

  //DONE: wrap IDisposable into using
  using (SqlCommand SelectCommand = new SqlCommand(sql, conn)) {
    using (var myreader = SelectCommand.ExecuteReader()) {
      while (myreader.Read()) {
        var ob = new StrukturaObrok(
          // Conversion instead of Cast
          Convert.ToInt32(myreader["Pin"]),

          Convert.ToString(myreader["Name"]),
          Convert.ToString(myreader["Surname"]),
          Convert.ToString(myreader["M1"]),
          Convert.ToString(myreader["M2"]),
          Convert.ToString(myreader["M3"]),
          Convert.ToString(myreader["M4"]),            

          Convert.ToInt32(myreader["M1Cijena"]),
          Convert.ToInt32(myreader["M2Cijena"]),
          Convert.ToInt32(myreader["M3Cijena"]),
          Convert.ToInt32(myreader["M4Cijena"]),  

          Convert.ToString(myreader["UkupnoM1_M2"]),
          Convert.ToString(myreader["UkupnoM3_M4"])
        );

        obrok.Add(ob); 
      }
    } 
  }
}

return obrok;