SqlCommand com = new SqlCommand("select top @line ArtId,title,filepath from article order by sendtime desc;");
SqlParameter param = new SqlParameter("@line",15);
总是出错,我猜应该是@line的数据类型问题,可是@line的数据类型不像列那么的明确,这里应该怎么处理?帮帮忙

解决方案 »

  1.   


    SqlCommand com = new SqlCommand("select top @line ArtId,title,filepath from article order by sendtime desc"); //将你sql语句后面的逗号去掉
    SqlParameter param = new SqlParameter("@line",15);
    com.Parameters.Add(param);
      

  2.   

    拼接字符串 string.Format("select top {0} ArtId,title,filepath from article order by sendtime desc",15)
      

  3.   

    "select top ( @line) ArtId,title,filepath from article order by sendtime desc
     @line 是int类型的,不知怎么不明确
    试试
      

  4.   

    SqlParameter param = new SqlParameter("@line",SqlDbType.Int,15);
      

  5.   


    哪个逗号?好像不是逗号的问题,我在写sql语句不用参数而是用具体数字(比如15)去代替的时候,就能成功.
      

  6.   

            public DataTable GetTable()
            {
                SqlCommand com = new SqlCommand("select top @line ArtId,title,lwcool_filepath from article order by sendtime desc;");
                SqlParameter param = new SqlParameter("@line",SqlDbType.Int);
                param.Value = this.num;
                com.Parameters.Add(param);
                com.Connection = Connection.CreateConnection(this.server);
                SqlDataAdapter adapter = new SqlDataAdapter(com);
                DataTable table = new DataTable("new_message");
                table.Columns.Add("artid");
                table.Columns.Add("title");
                table.Columns.Add("filepath");
                
                DataTableMapping table_mapding=adapter.TableMappings.Add("Table", "new_message");
                table_mapding.ColumnMappings.Add("ArtId","artid");
                table_mapding.ColumnMappings.Add("title","title");
                table_mapding.ColumnMappings.Add("lwcool_filepath","filepath");
                adapter.Fill(table);
                return table;
            }这是源码,好像不是int类型,string也试过,没用我不想用字符串拼接,用参数感觉比较好,
      

  7.   

    select top (@line)
    这里要加上括号。
      

  8.   

    param.Value = this.num;这里你的this.num;返回的是什么值?是int型的不是?SqlParameter param = new SqlParameter("@line",SqlDbType.Int,4);
    param.Value = int.Parse(this.num); //如果是字符串的话,请转型
    com.Parameters.Add(param);