четвъртък, 20 януари 2011 г.

Get Data From MS SQL Database with ADO.NET C#

In the code-behind you have: 
-------------------------------
using System.Data.SqlClient;
using System.Data;
string yourSqlQuery = "SELECT ID, Decsription FROM Articles Where ManufacturerName = @ParamName";
string connString = "Data Source=ServerName\\SqlInstance;Database=dbName;User=UserAcc;password=Pwd;Integrated Security=False";

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(yourSqlQuery, conn);
cmd.Parameters.AddWithValue("@ParamName", "paramValue");
if (conn.State != ConnectionState.Open)
    conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
------------------------------
Note: Account "UserAcc" used to run the query needs permissions in the SqlServer -> Security -> Logins. In case you use Integrated Security=True you actually use Network Services account (or ASPNET in Win XP) and should consider granting necessary permissions. Have in mind that firewalls, routers can also block your connection to the SqlServer.

Няма коментари:

Публикуване на коментар