C# 从 SQL Server 数据库获取数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9412277/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:17:07  来源:igfitidea点击:

Obtaining a dataset from a SQL Server database

c#asp.net.netsql-servervisual-studio

提问by user559142

I am a newbie to C# and I need to obtain a dataset from a dummy database I have made. I'm used to coding in objective-c and using php/mysql to deal with the data insertion/extraction....

我是 C# 的新手,我需要从我制作的虚拟数据库中获取数据集。我习惯在objective-c中编码并使用php/mysql来处理数据插入/提取......

Can anyone tell me how to obtain an entire table of data from a SQL Server database? Or at least point me in the direction of a reliable defacto source?

谁能告诉我如何从 SQL Server 数据库中获取整个数据表?或者至少指出我可靠的事实来源的方向?

采纳答案by adatapost

You have to use provider for MSSQL - SqlDataAdapter class.

您必须为 MSSQL- SqlDataAdapter 类使用提供程序。

string CnStr=@"put_here_connection_string";
SqlDataAdapter adp=new SqlDataAdapter("select * from tableName",CnStr);
DataSet ds=new DataSet();
adp.Fill(ds,"TableName");

回答by silverfighter

This should get you started:

这应该让你开始:

Here is a video what you are looking for but not up to date: http://www.asp.net/web-forms/videos/how-do-i/how-do-i-create-data-driven-web-sites

这是您正在寻找但不是最新的视频:http: //www.asp.net/web-forms/videos/how-do-i/how-do-i-create-data-driven-web -站点

maybe you should check out the getting started section: http://www.asp.net/web-forms/videos

也许您应该查看入门部分:http: //www.asp.net/web-forms/videos

Another choice would be ASP.NET MVC if you have some MVC insides (Rails, PHP, etc) http://www.asp.net/mvc

如果您有一些 MVC 内部结构(Rails、PHP 等),另一种选择是 ASP.NET MVC http://www.asp.net/mvc

This is a complete walk through application:

这是一个完整的演练应用程序:

http://www.asp.net/mvc/tutorials/mvc-music-store

http://www.asp.net/mvc/tutorials/mvc-music-store

I would recommend looking at mvc and the music store example HTH

我建议查看 mvc 和音乐商店示例 HTH

回答by dash

There are plenty of ways to do this; look at tutorials like:

有很多方法可以做到这一点;看看这样的教程:

Introduction to SqlClient

SqlClient 介绍

A typical code example to just return a data table might look like:

仅返回数据表的典型代码示例可能如下所示:

public static DataTable GetTable(string tableName, string connectionString)
{

  using (SqlConnection myConnection = new SqlConnection(connectionString))
  {
    using (SqlCommand myCommand = new SqlCommand(tableName))
    {
      myCommand.Connection = myConnection;
      myCommand.CommandType = CommandType.TableDirect;
      using (SqlDataReader reader = myCommand.ExecuteReader())
      {
        DataTable table = new DataTable();
        table.Load(reader);
        return table;
      }
    }

  }
}

Note the use of the usingkeyword. This will ensure your connection is disposed when you are finished with it.

请注意using关键字的使用。这将确保您的连接在您完成后被处理。

There's example code for obtaining a DataSet here.

有获得一个DataSet示例代码在这里

You can also vary how you execute your command; you can use myCommand.CommandType = CommandType.Textand set the CommandString to "SELECT * FROM myTable". You can also use CommandType.StoredProcedureand use the name of a stored procedure.

您还可以改变执行命令的方式;您可以使用myCommand.CommandType = CommandType.Text并将 CommandString 设置为"SELECT * FROM myTable". 您还可以使用CommandType.StoredProcedure和使用存储过程的名称。

You might also want to look at abstracting all of this away using one of the many solutions available. Microsoft have the Application Data blocks, Entity Framework, and there are plenty of other alternatives.

您可能还想考虑使用许多可用的解决方案之一来抽象所有这些。微软有应用程序数据块实体框架,还有很多其他的选择。

回答by Maheep

See this on MSDN

MSDN上看到这个

using (SqlConnection conn = new SqlConnection("CONNECTION_STRING"))
{
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand("TbaleName", conn)
                                { CommandType = CommandType.Table };
    adapter.Fill(dataset);
    return dataset;
}

To know more about what to write in CONNECTION_STRINGsee this

要了解有关要写的内容的更多信息,CONNECTION_STRING请参阅此