1. The statements to the database parameter passing

to the database operation statements passing parameters can be realized through stored procedures, there are two other simple easyjet methods:

can be passed in c #string manipulation parameters directly into the SQL statement variables, such as:

string s="Davolio";

string SQL="select * from employees where LastName=" + "'" + s + "'"

is equivalent to write SQL statements:

select * from employees where LastName='Davolio'
      But can be by thisCommand. The Parameters. The Add () method, as shown in the following:

string s="Davolio";


SqlConnection thisConnection=new SqlConnection

(" Data Source=(local); Initial Catalog=Northwind; UID=sa; PWD=");

thisConnection. The Open ();

SqlCommand thisCommand=thisConnection. CreateCommand ();


thisCommand.Com mandText=

"select * from employees where LastName=@ ";

thisCommand. The Parameters. The Add (" @ ", s);

you can see, the string s pass parameters "Ddbolio" to the parameters in the database operation statements .

2. Read in different tables within the data in the database to the data set DataSet

SqlDataAdapter's Fill method can Fill the known data set, and for each Fill item to create a temporary table, can access to the table to read the data set of related data. Its related operation is as follows:


SqlConnection thisConnection=new SqlConnection

(" Data Source=(local); Initial Catalog=Northwind; UID=sa; PWD=");

try

the {

thisConnection. The Open ();

the }

catch Exception (ex)

the {

thisConnection. Close ();

the }

 

string sql1="select * from employees".

string sql2="select * from Customers".

SqlDataAdapter sda=new SqlDataAdapter (sql1, thisConnection);

the DataSet ds=new DataSet ();

sda. The Fill (ds, "myemployees");

sda. The Dispose ();

 

SqlDataAdapter sda1=new SqlDataAdapter (sql2, thisConnection);

sda1. The Fill (ds, "myCustomers");

sda1. The Dispose ();

string t1=ds. The Tables [r]. "myemployees" Rows [0] [r]. "Hiredate" ToString ();

string t2=ds. The Tables [r]. "myCustomers" Rows [0] [r]. "ContactTitle" ToString ();

 

Page. RegisterStartupScript (" aa ", "<script language=javascript> alert (' t1=" + t1 + ", t2="+ t2 +" '); </script> ");


you can see, in the newly created two temporary table data set ds "myemployees" and "myCustomers. For validation data in these two tables have really read the ds data sets, through data read operations will be corresponding to the attribute table "myemployees" "Hiredate" assigned to the first line of the character variables t1, the table corresponding to the attribute "ContactTitle" in "myCustomers" assigned to the first line of the character variables t2, and through the JavaStript function "alert ()," will these variables are displayed in the pop-up window. Page. RegisterStartupScript method is used to send the client script block, it marked the first parameter, the user can freely choose, the second parameter for JavaScript, alert function used to pop up here MessageBox dialog, we'll parameters t1 and t2 to the script, make its in MessageBox display.

ps: because the network speed too slow, not displaying the related chart to the server, is really one big regret. And don't know what to write code style and format, make the code presented appears very messy.


This concludes the body part