如何在C#中访问数据库
基本上,我想简要解释一下如何使用Ccode访问SQL数据库。我收集到一个连接和一个命令是必需的,但是这是怎么回事?我想我要问的是让某人消除这个过程的神秘感。谢谢。
为了清楚起见,在我的情况下,我正在做Web应用程序,电子商务之类的事情。全部都是ASP.NET,C#和SQL数据库。
我要继续并关闭该线程。这有点笼统,我将针对该主题发布更多针对性和教程式的问题与解答。
解决方案
要看的主题:
- ADO.NET基础
- LINQ转SQL
- 托管数据库提供者
MSDN在这里有相当不错的文章:
http://msdn.microsoft.com/zh-CN/library/s7ee2dwt(VS.71).aspx
我们应该看一下数据读取器中的简单选择语句。来自MSDN页面的示例:
private static void ReadOrderData(string connectionString) { string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;"; using (SqlConnection connection = new SqlConnection( connectionString)) { SqlCommand command = new SqlCommand( queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); try { while (reader.Read()) { Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1])); } } finally { // Always call Close when done reading. reader.Close(); } } }
基本上,它首先创建一个SqlConnection对象,然后创建一个SqlCommand对象,该对象保存我们将要执行的实际选择,以及对我们刚刚创建的连接的引用。然后打开连接,在下一行,执行语句并返回SqlDataReader对象。
然后在while循环中,它从读取器的第一行输出值。每次调用" reader.Read()"时,阅读器都会包含一个新行。
然后关闭阅读器,由于我们要退出"使用"秘密,连接也将关闭。
编辑:如果我们正在寻找有关选择/更新ASP.NET中的数据的信息,4GuysFromRolla在ASP.NET 2.0的数据源控件上有一个非常不错的Multipart系列
EDIT2:正如其他人指出的那样,如果我们使用的是.NET的较新版本,我建议我们研究LINQ。可以在此MSDN页面上找到简介,示例和文章。
如果它是一个Web应用程序,那么这里有一些很好的资源供我们开始.NET中的数据访问:
http://weblogs.asp.net/scottgu/archive/2007/04/14/working-with-data-in-asp-net-2-0.aspx
读起来像一个初学者的问题。那需要初学者视频演示。
http://www.asp.net/learn/data-videos/
它们是ASP.NET的重点,但要注意数据库方面。
要在SQL Server数据库上连接/执行操作:
using System.Data; using System.Data.SqlClient; string connString = "Data Source=..."; SqlConnection conn = new SqlConnection(connString); // you can also use ConnectionStringBuilder connection.Open(); string sql = "..."; // your SQL query SqlCommand command = new SqlCommand(sql, conn); // if you're interested in reading from a database use one of the following methods // method 1 SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { object someValue = reader.GetValue(0); // GetValue takes one parameter -- the column index } // make sure you close the reader when you're done reader.Close(); // method 2 DataTable table; SqlDataAdapter adapter = new SqlDataAdapter(command); adapter.Fill(table); // then work with the table as you would normally // when you're done connection.Close();
大多数其他数据库服务器(如MySQL和PostgreSQL)具有相似的接口用于连接和操作。
如果我们要寻找的是易于遵循的教程,则应转到www.ASP.net网站。
这里是到入门视频页面的链接:http://www.asp.net/learn/videos/video-49.aspx
如果要下载,请参见以下视频:视频下载
这是从视频到Cproject的链接:下载项目
祝你好运。
我也建议使用数据集。它们非常易于使用,只需单击几下鼠标,而无需编写任何代码,并且对于小型应用程序已经足够好了。
如果我们使用的是Visual Studio 2008,建议我们跳过ADO.NET,直接跳到LINQ to SQL
旧的ADO.Net(sqlConnection等)是LINQ出现的一种恐龙。 LINQ需要.Net 3.5,但向后兼容所有.Net 2.0+和Visual Studio 2005等。
从linq开始非常容易。
- 向项目添加一个新项目,即linq-to-sql文件,该文件将放置在App_Code文件夹中(对于本示例,我们将其称为example.dbml)
- 从服务器资源管理器中,将一个表从数据库中拖到dbml中(在本示例中,该表将被命名为items)
- 保存dbml文件
我们现在已经建立了一些类。我们构建了exampleDataContext类,它是linq初始化程序,并且构建了item类,它是items表中对象的类。这一切都是自动完成的,我们无需担心。现在说我想获得itemID为3的记录,这是我要做的全部事情:
exampleDataContext db = new exampleDataContext(); // initializes your linq-to-sql item item_I_want = (from i in db.items where i.itemID == 3 select i).First(); // using the 'item' class your dbml made
这就是全部。现在,我们有了一个名为item_I_want ...的新项目,如果我们想从该项目中获取一些信息,可以这样称呼它:
int intID = item_I_want.itemID; string itemName = item_I_want.name;
Linq的使用非常简单!这只是冰山一角。
当我们拥有更强大,更轻松的工具时,无需学习过时的ADO :)
@J D OConal基本上是正确的,但是我们需要确保处置连接:
string connString = "Data Source=..."; string sql = "..."; // your SQL query //this using block using( SqlConnection conn = new SqlConnection(connString) ) using( SqlCommand command = new SqlCommand(sql, conn) ) { connection.Open(); // if you're interested in reading from a database use one of the following methods // method 1 SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { object someValue = reader.GetValue(0); // GetValue takes one parameter -- the column index } // make sure you close the reader when you're done reader.Close(); // method 2 DataTable table; SqlDataAdapter adapter = new SqlDataAdapter(command); adapter.Fill(table); // then work with the table as you would normally // when you're done connection.Close(); }