Tips for manipulating SQL Server in asp.net

Source: Internet
Author: User
Tags sql first row variable tostring
asp.net|server|sql| Tips 1. Pass to database statement parameters
  
Passing parameters to a database operation statement can be implemented by a stored procedure, and here are two other easy ways to do this:
  
You can pass parameters directly into a SQL statement variable in C # by using string manipulation, for example:
  
String s= "Davolio";
  
String Sql= "SELECT * FROM Employees where lastname=" + "" "+s+" "
  
Equivalent to writing to SQL statements:
  
SELECT * FROM Employees where lastname= ' Davolio ' can also be passed through Thiscommand. The Parameters.Add () method is implemented as follows:
  
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 action statement.
  
   2. Read data from different tables in the database into dataset datasets
  
The SqlDataAdapter Fill method fills a known dataset and creates a temporary table for each fill item that can be read from the data set by access to that table. The related actions 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, in the DataSet DS, new students become two temporary tables "myemployees" and "mycustomers". To verify that the data in the two tables are indeed read into the DataSet DS, assign the first row of the table "MyEmployees" in the Tables "HireDate" to the character variable T1 with the data read operation, and the table "Mycustomers" corresponds to the property "ContactTitle" The first line is assigned to the character variable T2, and the variables are displayed in the pop-up window by the Javastript function "alert ()". The Page.registerstartupscript method is used to emit a client script block whose first parameter is a flag bit, the user can select any, and the second parameter is a JavaScript script, where the alert function pops up the MessageBox dialog box, and we pass the parameters T1 and T2 into the script So that it is displayed in the MessageBox.
  
PS: Because the network speed is too slow, the relevant display chart can not be uploaded to the server, really a great regret. There's also the style and format of not knowing how to write code that makes the code look messy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.