C# 在方法中将数据集作为对象返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11598094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning a Dataset as an Object in a Method
提问by unknownsatan
I am writing a method that will query a table and return a Datasetobject containing the specified column. Moreover, I have a problem with my Username & Password, so I am using Windowsauthentication for the same but I am not too sure about that in my snippet I have written till now.
我正在编写一个方法来查询一个表并返回一个包含指定列的Dataset对象。此外,我的用户名和密码有问题,所以我正在使用Windows身份验证,但我在我写的代码片段中对此不太确定。
protected void GetProgramList()
{
SqlConnection cn = new SqlConnection("server=Daffodils-PC/sqlexpress;Database=Assignment1;Trusted_Connection=Yes;");
SqlCommand cmd = new SqlCommand("SELECT ProgramName FROM Program", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet1 ds1 = new DataSet1();
}
I have been trying to follow official MS documentation but I am not sure where I am going? Can someone help me with some links or snippets?
我一直在尝试遵循 MS 官方文档,但我不确定我要去哪里?有人可以帮我提供一些链接或片段吗?
采纳答案by Mitja Bonca
I would say you have 2 options here: 1. to make a DataSet class variable, so its reference can be accessed from all over the class (set its access modifier to public so it can be accessed from other classes) 2. or create a method with its return type of DataSet. But in this case on the other side must be set to receive the DataSet as well:
我想说你在这里有 2 个选项: 1. 创建一个 DataSet 类变量,以便可以从整个类访问它的引用(将其访问修饰符设置为 public,以便可以从其他类访问它) 2. 或创建一个方法,其返回类型为 DataSet。但在这种情况下,另一端也必须设置为接收 DataSet:
//2. solution:
//2. 解决方案:
private void GetData()
{
//from inside some method:
DataSet ds = GetProgramList();
}
protected DataSet GetProgramList()
{
DataSet ds1 = new DataSet();
using (SqlConnection cn = new SqlConnection("server=Daffodils-PC/sqlexpress;Database=Assignment1;Trusted_Connection=Yes;"))
{
using (SqlDataAdapter da = new SqlDataAdapter(@"SELECT ProgramName FROM Program", cn))
da.Fill(ds1, "TableName1");
}
return ds1;
}
//
//1. solution:
class YourClass
{
DataSet ds1;
protected void GetProgramList()
{
SqlConnection cn = new SqlConnection("server=Daffodils-PC/sqlexpress;Database=Assignment1;Trusted_Connection=Yes;");
SqlCommand cmd = new SqlCommand("SELECT ProgramName FROM Program", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds1 = new DataSet();
}
}
回答by paulsm4
Suggestion: "use" System.Data and System.Data.SqlClient, and use a "SqlDataReader":
建议:“使用”System.Data 和 System.Data.SqlClient,并使用“SqlDataReader”:
Either read everything in your routine (generally preferred), or pass the SqlDataReader back to the caller (as a function return).
要么读取例程中的所有内容(通常首选),要么将 SqlDataReader 传递回调用者(作为函数返回)。
And be sure to .Close() the reader when you're done :)
并确保在完成后 .Close() 阅读器:)
回答by Rohit Vats
SQLDataAdapter basicwill get you started with the basics of creating a connection and consuming it in your code.
SQLDataAdapter basic将使您开始了解创建连接并在代码中使用它的基础知识。
回答by A Ghazal
Place your connection string in AppSettings section in app.config or web.config
将您的连接字符串放在 app.config 或 web.config 的 AppSettings 部分
public string GetSqlConnection()
{
return System.Configuration.ConfigurationManager.AppSettings["SqlConnectionString"];
}
public DataSet getDataSet(string sql)
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(GetSqlConnection());
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
conn.Close();
conn.Dispose();
da.Dispose();
return ds;
}

