1

我正在尝试从 Visual Studio 中的 C# 项目连接到 CRM SQL 服务器。

我已经将数据库添加为数据连接,但我不知道如何在代码中连接到它以及如何进行示例查询(例如SELECT * FROM FilteredAccount

有什么建议吗?

4

1 回答 1

2

如果要连接到本地 SQL Server Express,并连接到“Northwind”数据库,并从“客户”表中读取前 5 个客户,则必须执行以下操作:

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():

   }
}
于 2013-09-11T07:57:33.063 回答