如何从sql server为通用程序执行存储过程? [英] How do I execute a stored procedure from sql server for a generic program?

查看:126
本文介绍了如何从sql server为通用程序执行存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我将非常感谢您的帮助:)



如何从sql server执行存储过程并让它返回表到程序并根据存储过程返回的数据表的数量生成所需的csv文件数?



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
SqlConnection sqlConnection1 = new SqlConnection( 您的连接字符串);
SqlCommand cmd = new SqlCommand();
SqlDataReader阅读器;

cmd.CommandText = 测试;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();

sqlConnection1.Close();
}
}
}





我收到的当前错误:

错误:当前上下文中不存在名称'CommandType'





我是否必须向App.config添加任何代码?使用当前执行代码,我能够用它生成csv文件,如何根据代码生成csv文件而无需任何用户输入?

解决方案

为了修复错误,您需要使用系统添加

  .Data.SqlClient; 



或者你需要指定类的命名空间,例如

系统.Data.SqlClient.SqlConnection sqlConnection1 =  new  System.Data.SqlClient.SqlConnection( 您的连接字符串); 



还要确保System.Data程序集包含在项目引用中。


1。包括system.Data和System.Data.SqlClient的using语句如下所示

 使用 System.Data; 
使用 System.Data.SqlClient;





2。使用存储过程填充数据集

 使用(SqlConnection conn =  new  SqlConnection(  ConnectionString))
{
SqlCommand sqlComm = new SqlCommand( StoredProcedureName,conn);
sqlComm.CommandType = CommandType.StoredProcedure;

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



3.现在您使用多个表调用数据集,使用将DataTable写入CSV文件 [ ^ ]


Hi, I am new to C#, I will kindly appreciate your help:)

How do I execute a stored procedure from sql server and let it return tables to the program and generates number of csv file required based on the number of data tables return from the Stored procedure?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;

            cmd.CommandText = "Test";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();

            reader = cmd.ExecuteReader();

            sqlConnection1.Close();
        }
    }
}



The current errors I received:

Error: The name 'CommandType' does not exist in the current context	



Do I have to add any codes to the App.config? With the current execution codes, am I able to generate csv files with it and how do I go about generating csv files based on the codes without the need of any user input?

解决方案

In order to fix the errors, you need to add

using System.Data.SqlClient;


or you need to specify the namespace for the classes, for example

System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("Your Connection String");


Also ensure that System.Data assembly is included in your project references.


1. Include using statements for system.Data and System.Data.SqlClient as below

using System.Data;
using System.Data.SqlClient;



2. Fill Data Set using stored procedure

using(SqlConnection conn = new SqlConnection("ConnectionString"))
{
        SqlCommand sqlComm = new SqlCommand("StoredProcedureName", conn);
        sqlComm.CommandType = CommandType.StoredProcedure;

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


3. Now you have data set called with multiple tables, export each DataTable to CSV file using Writing a DataTable to a CSV File[^]


这篇关于如何从sql server为通用程序执行存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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