如何创建从sql server返回表的程序? [英] How do I create a program that return tables from sql server?

查看:90
本文介绍了如何创建从sql server返回表的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection conn = new SqlConnection("ConnectionString"))
            {

                SqlCommand sqlComm = new SqlCommand("Test", conn);

                sqlComm.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(sqlComm);
                DataSet ds = new DataSet();
                da.Fill(ds);
            }
        }

    }
}

推荐答案

1。首先尝试自己,如果你有正确的连接字符串和返回多个表的存储过程,你将得到带有多个表的DataSet。



2. DataSet可以有多个DataTables,如果存储过程返回多个表,则可以使用数据集Tables属性访问每个表。您可以将DataTable保存为CSV文件,只需google for it,您将找到许多解决方案和相关代码示例。



3.您可以迭代数据集中的所有数据表并在步骤2中找到导出为CSV代码的每个数据表。



4.保存csv文件时,给出带有日期时间戳的名称,检查< a href =http://stackoverflow.com/questions/7898392/append-timestamp-to-a-file-name/7898476#7898476>将TimeStamp附加到文件名 [ ^ ]
1. Try yourself first, if you have proper connection string and stored procedure which returning multiple tables, you will get DataSet with multiple tables.

2. DataSet can have multiple DataTables, if your stored procedure returning multiple tables, then you can access each table by using your dataset Tables property. You can save DataTable to CSV file, just google for it, you will find many solutions and related code samples.

3. you can iterate all the Datatables in the dataset and call each datatable with export to CSV code which you find on step 2.

4. when you save csv file, give the name with date time stamp, check Append TimeStamp to a File Name[^]


要回答是否可以使用上面的代码创建CSV,请执行。你需要更多的编码。看看 C# - CSV导入导出 [ ^ ]。这是一个非常好的例子,说明如何从SQL Server中的数据创建CSV。注意 exportToCSVfile 方法。
To answer if you can create a CSV with the code above, no. You need a lot more coding. Have a look at C# - CSV Import Export[^]. It's a very nice example how to create a CSV from data in SQL Server. Pay attention to exportToCSVfile method.


这篇关于如何创建从sql server返回表的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆