我如何连接到从C#中的SQL数据库?连接到、数据库、SQL

2023-09-02 01:56:44 作者:當時只道是尋常

我想写一个本地程序的管理和安装系统,我的家庭网络,我想我得确定下来的技术:

I am trying to write a local program management and install system for my home network, and i think i've got the technologies nailed down:

在C#/。NET / WPF客户端 Lua的安装脚本支持(通过LuaInterface) 在SQL Server的防爆preSS维护程序的数据库

但是我不能确定具体是什么我会使用C#连接到数据库。是不是有什么内置在.NET框架呢?奖励积分,如果你对我应该用什么与该数据库交互的建议。

However i'm unsure what specifically i'll use to connect C# to the database. Is there something built into the .NET framework for this? Bonus points if you have a suggestion on what i should use for interacting with said database.

推荐答案

查看

介绍ADO.NET教程 ADO.NET教程第1课 介绍到ADO.NET Introduction to ADO.NET Tutorial ADO.NET Tutorial Lesson 1 An introduction to ADO.NET

我敢肯定有很多更在那里 - 只是谷歌ADO.NET和教程......

I'm sure there's plenty more out there - just google for "ADO.NET" and "Tutorial" ......

更新:

如果你想连接到您的本地SQL Server防爆preSS,并连接到罗斯文数据库,并从客户表中读出前5名客户,你必须做这样的事情

If you want to connect to your local SQL Server Express, and connect to the "Northwind" database, and read the top 5 customers from the "Customers" table, you'd have to do something like this:

string connectionString = "server=(local)SQLExpress;database=Northwind;integrated Security=SSPI;";

using(SqlConnection _con = new SqlConnection(connectionString))
{
   string queryStatement = "SELECT TOP 5 * FROM dbo.Customers ORDER BY CustomerID";

   using(SqlCommand _cmd = new SqlCommand(queryStatement, _con))
   {
      DataTable customerTable = new DataTable("Top5Customers");

      SqlDataAdapter _dap = new SqlDataAdapter(_cmd);

      _con.Open();
      _dap.Fill(customerTable);
      _con.Close();

   }
}

现在你将有所有5个顶级客户从数据表中的Northwind数据库,您可以检查它们,把它们打印出来,操控起来 - 任何你想做的事情

Now you would have all 5 top customers from your Northwind database in the DataTable and you can inspect them, print them out, manipulate them - whatever you want to do.

这是ADO.NET在行动!

That's ADO.NET in action!

作为连接字符串的细节 - 什么样的选择,你可以使用,它应该是什么样子,请查看连接字符串网站 - 它拥有吨的例子和说明

As for the details of the connection string - what options you can use and what it should look like, check out the Connection Strings web site - it has tons of examples and explanations.

马克·