How To Describe Connection To SQL Server: Using Using
How To Describe Connection To SQL Server: Using Using
using System.Data;
using System.Data.SqlClient;
Keyword Description
The address of a SQL Server. If the
server is on a same computer, where
your website runs, define it as "local". If
server the server is remote, define it as an IP
– address, a domain name or a
netbios name (as in the example string)
of the server.
The login name, which is defined at
Uid your SQL Server to get access. Our
login name is "sa".
The password, which is defined at your
Pwd SQL Server to get access. Our password
is "sa".
1
The database's name, which you
Database connect to. Our database name is
"GAZCAD".
The time in milliseconds. When this
time is over and the connection is not
established, the timeout exception is
Connect
thrown. This keyword is not necessary.
timeout
In our case it equals 10,000 ms. Use so
large timeouts when you request a lot
of data from the server.
myTable
Field Description
Id INT, Primary Key
Value INT
2
statements and procedures, and close the connection. Here
is the code snippet for that:
myProc
Parameter Description
@Id Input, INT
@Value Input, CHAR(10)
@Ret Output, INT
3
Select" statements and stored procedures. The command
string contains the procedure's name now. The
CommandType Property has to be set as StoredProcedure
(use the CommandType enumeration), because the default
is Text (T-SQL statement). To create the parameter list we
use the SQLParameter class. To set a type of parameters,
we use the SQLDbType enumeration. To set a direction of a
parameter we use the ParameterDirection enumeration.
There is the snippet with comments below:
4
// execute the procedure
execproc.ExecuteNonQuery();
// Get a value of “@Ret†(it is Output). Don`t forget
cast the Value property //to a required type, cause the Value
property has the Object type.
int ret = (int)execproc.Parameters["@Ret"].Value;
//Close our connection
execproc.Connection.Close();
5
The "ds" contains findings now. You will find out how to
process them at the next step.
Process findings
We have got the "ds" instance of the Dataset with findings
at the previous step. So, we should process them to display
at our web site. The Dataset contains the Tables property. It
is the collection of tables. Findings are written to a zero-
indexed table. We use two loops to seek all data. The first
loop seeks all rows in a table (DataRow instances); the
embedded loop seeks all columns in a row (DataColumn
instances). You can get each element as
DataRow[Datacolumn]. There is the snippet with comments
below: