1. Pass parameters
to database statements. Passing parameters to database operation statements can be achieved through stored procedures. Here are two other simple and easy methods:
parameters can be directly passed into SQL statement variables through string operations in C#, for example :
string s="Davolio";
string sql= "select * from employees where LastName="+"'"+s+"'"
is equivalent to writing a SQL statement:
select * from employees where LastName='Davolio'
It can also be achieved through thisCommand.Parameters.Add() method, as shown below:
string s="Davolio";
SqlConnection thisConnection=new SqlConnection
("Data Source=(local);Initial Catalog=Northwind;UID=sa;PWD=");
thisConnection.Open ();
SqlCommand thisCommand=thisConnection.CreateCommand ();
thisCommand.CommandText =
" select * from employees where LastName=@charname ";
thisCommand.Parameters.Add("@charname",s);
As you can see, the string s passes the parameter "Ddbolio" to the parameter charname in the database operation statement.
2. Read data from different tables in the database into the data set DataSet.
The Fill method of SqlDataAdapter can fill the known data set and create a temporary table for each filled item. The data can be read by accessing the table. Centralized relevant data. The related operations are as follows:
SqlConnection thisConnection=new SqlConnection
("Data Source=(local);Initial Catalog=Northwind;UID=sa;PWD=");
try
{
thisConnection.Open ();
}
catch(Exception ex)
{
thisConnection.Close ();
}
string sql1="select * from employees";
string sql2="select * from Customers";
SqlDataAdapter sda=new SqlDataAdapter(sql1,thisConnection);
DataSet ds= new DataSet();
sda.Fill(ds,"myemployees");
sda.Dispose();
SqlDataAdapter sda1=new SqlDataAdapter(sql2,thisConnection);
sda1.Fill(ds,"myCustomers");
sda1.Dispose();
string t1=ds.Tables["myemployees"].Rows[0]["Hiredate"].ToString();
string t2=ds.Tables["myCustomers"].Rows[0]["ContactTitle"].ToString( );
Page.RegisterStartupScript("aa","<script language=javascript>alert('t1="+t1+",t2="+t2+"');</script>");
As you can see, two temporary tables "myemployees" and "myCustomers" are newly generated in the data set ds. In order to verify that the data in these two tables has indeed been read into the data set ds, the first row corresponding to the attribute "Hiredate" in the table "myemployees" is assigned to the character variable t1 through the data reading operation, and the table "myCustomers" The first line corresponding to the attribute "ContactTitle" is assigned to the character variable t2, and these variables are displayed in the pop-up window through the JavaStript function "alert()". The Page.RegisterStartupScript method is used to issue a client script block. Its first parameter is a flag, which the user can choose arbitrarily. The second parameter is a JavaScript script. Here the alert function is used to pop up the MessageBox dialog box. We set the parameters t1 and t2. Pass it into the script to display it in the MessageBox.
ps: Because the network speed is too slow, the relevant display charts cannot be transmitted to the server, which is a real pity. There is also a lack of knowledge of the style and format of writing code, which makes the code given appear messy.