0

我对 ASP.net 和存储过程有疑问

我在 SQL Server 中的过程:

ALTER PROCEDURE [dbo].[top1000]
            @Published datetime output,
            @Title nvarchar(100) output,
            @Url nvarchar(1000) output,
            @Count INT output

AS 
SET @Published = (SELECT TOP 1000  dbo.vst_download_files.dfl_date_public FROM dbo.vst_download_files
    ORDER BY dbo.vst_download_files.dfl_download_count DESC )
SET @Title = (SELECT TOP 1000  dbo.vst_download_files.dfl_name FROM dbo.vst_download_files
    ORDER BY dbo.vst_download_files.dfl_download_count DESC) 
SET @Url = (SELECT TOP 1000  dbo.vst_download_files.dfl_source_url FROM dbo.vst_download_files
    ORDER BY dbo.vst_download_files.dfl_download_count DESC) 
SET @Count = (SELECT TOP 1000  dbo.vst_download_files.dfl_download_count FROM dbo.vst_download_files
    ORDER BY dbo.vst_download_files.dfl_download_count DESC) 

还有我在网站项目中的程序

public static void Top1000()
{
        List<DownloadFile> List = new List<DownloadFile>();
        SqlDataReader dbReader;

        SqlParameter published = new SqlParameter("@Published", SqlDbType.DateTime2);
        published.Direction = ParameterDirection.Output;
        SqlParameter title = new SqlParameter("@Title", SqlDbType.NVarChar);
        title.Direction = ParameterDirection.Output;
        SqlParameter url = new SqlParameter("@Url", SqlDbType.NVarChar);
        url.Direction = ParameterDirection.Output;
        SqlParameter count = new SqlParameter("@Count", SqlDbType.Int);
        count.Direction = ParameterDirection.Output;
        SqlParameter[] parm = {published, title, count};

        dbReader = MsSqlProvider.ExecProcedure("top1000", parm);

        try
        {
            while (dbReader.Read())
            {
                DownloadFile df = new DownloadFile();
                //df.AddDate = dbReader["dfl_date_public"];
                df.Name = dbReader["dlf_name"].ToString();
                df.SourceUrl = dbReader["dlf_source_url"].ToString();
                df.DownloadCount = Convert.ToInt32(dbReader["dlf_download_count"]);
                List.Add(df);
            }

            XmlDocument top1000Xml = new XmlDocument();
            XmlNode XMLNode = top1000Xml.CreateElement("products");

            foreach (DownloadFile df in List)
            {
                XmlNode productNode = top1000Xml.CreateElement("product");

                XmlNode publishedNode = top1000Xml.CreateElement("published");
                publishedNode.InnerText = "data dodania";
                XMLNode.AppendChild(publishedNode);

                XmlNode titleNode = top1000Xml.CreateElement("title");
                titleNode.InnerText = df.Name;
                XMLNode.AppendChild(titleNode);
            }

            top1000Xml.AppendChild(XMLNode);
            top1000Xml.Save("\\pages\\test.xml");
        }
        catch
        {
        }
        finally
        {
            dbReader.Close();
        }
    }

如果我做到了,MsSqlProvider.ExecProcedure("top1000", parm);我得到了

String[1]:属性 Size 的大小为 0 无效。

我应该在哪里寻找解决方案?程序还是方法?

4

1 回答 1

1

您需要为 url 和 Title 指定长度属性

SqlParameter title = new SqlParameter("@Title", SqlDbType.NVarChar); 
title.Size=1000

SqlParameter url = new SqlParameter("@Url", SqlDbType.NVarChar); 
url.Size=1000

您可以更改查询,而不是使用输出参数,如下所示

ALTER PRocedure [dbo].[top1000]
As
Begin 
Select top 1000 dfl_date_public ,dfl_name,dfl_source_url,
dfl_download_count from dbo.vst_download_files
order  by dbo.vst_download_files.dfl_download_count DESC 

然后使用执行阅读器

SqlCommand command =
        new SqlCommand("top1000", connection);
command.CommandType=CommandType.StoredProcedure
 SqlDataReader reader = command.ExecuteReader();
 // Iterate through reader as u did and add it to the collection 

使用 xelement 构建 XML

   foreach (DownloadFile df in List) 
        {
   XElement products=
new XElement("Products",
    new XElement("product",
        new XElement("published", "data dodania"),
        new XElement("title", df.Name) 
                 );
         }
于 2012-07-06T15:57:20.230 回答