Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要关于执行SQL和导出到excel的想法吗_C#_Sql Server_Stored Procedures_Export To Excel - Fatal编程技术网

C# 需要关于执行SQL和导出到excel的想法吗

C# 需要关于执行SQL和导出到excel的想法吗,c#,sql-server,stored-procedures,export-to-excel,C#,Sql Server,Stored Procedures,Export To Excel,现在,我的代码将查询的所有列导出到excel文件中。执行的SQL命令位于存储过程中。 我希望能够选择从SQL数据中指定要导出的列 我想到的一种方法是让用户能够选择他们想要的,并将它们连接到SQL查询中,而不是使用存储过程中的命令 我还尝试将参数放入select区域内的存储过程中,认为在select字段中包含参数很简单,但据我目前所知,由于可能存在SQL注入,因此不允许这样做 我见过一些人使用数据集和数据表保存SQL数据,然后将所有数据导出到excel的例子。但是,如果您这样使用它,在写出该列之前

现在,我的代码将查询的所有列导出到excel文件中。执行的SQL命令位于存储过程中。 我希望能够选择从SQL数据中指定要导出的列

我想到的一种方法是让用户能够选择他们想要的,并将它们连接到SQL查询中,而不是使用存储过程中的命令

我还尝试将参数放入select区域内的存储过程中,认为在select字段中包含参数很简单,但据我目前所知,由于可能存在SQL注入,因此不允许这样做

我见过一些人使用数据集和数据表保存SQL数据,然后将所有数据导出到excel的例子。但是,如果您这样使用它,在写出该列之前,您不需要检查一组条件语句吗

还有其他方法吗?下面发布的代码只是为了说明我目前正在做什么

private SqlDataSource GetDataSource(string FruitType)
{

    SqlDataSource tempDataSource = new SqlDataSource();
    tempDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["ServerConnectionString"].ToString();
    tempDataSource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
    switch (ReportType)
    {
        case "Oranges":
            tempDataSource.SelectCommand = "getOrange";                                          
            break;
        case "Apples":
            tempDataSource.SelectCommand = "getApples";                
            break;
        case "Pineapples":
            tempDataSource.SelectCommand = "getPineapples";
            break;
        case "Watermelons":                
            tempDataSource.SelectCommand = "getWatermelons";                
            break;
        case "GrapeFruit":                
            tempDataSource.SelectCommand = "getGrapeFruit";                
            break;
    }

    tempDataSource.DataBind();
    return tempDataSource;
}
protected void btnSaveData(object sender, EventArgs e)
{ 
    string attachment = "attachment; filename=";
    attachment += "Fruit Data";
    attachment += ".xls";

    Response.ClearContent();
    Response.Charset = "";
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    GridView gvTemp = new GridView();
    gvTemp.Attributes["runat"] = "server";

    SqlDataSource ds = GetDataSource(FruitType);
    gvTemp.DataSource = ds;
    gvTemp.DataBind();
    gvTemp.RenderControl(htw);
    Page.Controls.Add(gvTemp);
    Response.Write(sw.ToString());
    Response.End();

}

您可以轻松地使用临时表指定要从存储过程中获取哪些列:

CREATE PROCEDURE sp_GetDiffDataExample
      @columnsStatement NVARCHAR(MAX) -- Needed columns
AS
BEGIN
    DECLARE @query NVARCHAR(MAX)
    SET @query = N'SELECT ' + @columnsStatement + N' INTO ##TempTable FROM dbo.TestTable'
    EXEC sp_executeSql @query
    SELECT * FROM ##TempTable
    DROP TABLE ##TempTable
END

希望这能有所帮助。

有几个问题,但当我将变量放入columns参数时,它们是否需要已经是sql格式?例如'col name1,col name2',同样,为什么在从表中选择列后必须将其插入另一个表中,才能从测试表中选择全部?这不是有点多余吗?是的,columns参数必须是sql格式。这只是一个示例,所以您可以编写自己的参数并以相同的方式选择数据。据我所知,只有一个选项可以从程序中获得不同的列结果,而无需手动编码。若你们想让你们的用户选择你们想要的柱子,那个么这应该是最简单的例子。