Reputation: 1601
I am trying to connect to the CRM SQL server from a C# Project in Visual Studio.
I have already added the Database as a Data Connection, but I do not know how to connect to it in the code and how to make a sample query (e.g. SELECT * FROM FilteredAccount
)
Any suggestion?
Upvotes: 1
Views: 976
Reputation: 625
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():
}
}
Upvotes: 2