13th. Accessing the database using ADO

Source: Internet
Author: User
Tags finally block

I. ADO: Technology 1.ADO for connecting to a database. NET is divided into two components: DataSet. NET Framwork: Used to connect to the database, send commands, and retrieve the result 2.ADO. NET four core objects
Connection
Command
DataAdapter
DataReader two. Use ADO to access the database 1. First import the namespace System.Data.SqlClient 2. Create a connection string constr= "Data source=.;i Nitial Catalog=schooldb; User=sa; Password=. ";
If you do not have a password password parameter can be omitted 3. Create SqlConnection Connection object SqlConnection con=new SqlConnection (CONSTR); 4. Open database connection con.    Open (); Make sure that the database connection is open con before using the database.   Close (); To close the connection after you have finished using the database, release the resource three. Catch Exception try{
Put code that could be an exception in try}catch (exception type)
If an exception occurs in a try block, and the exception type matches the type of exception caught by the catch block, a catch is executed
{}finally{
No matter what happens, we go to the finally block.
}
Catching exceptions can catch exceptions without causing the program to stop four. Send a command to the database 1. Create SQL statement String sql= "SELECT COUNT (*) from Student Where studentname= ' +username+" '  and password= ' "+password+" ";  2. Send SQL commands using the Command object SqlCommand com=new SqlCommand (Sql,con); 3. Receive command execution result int count= (int) com.    ExecuteScalar (); ExecuteNonQuery () Executes statements that do not return rows, such as update, etc.
ExecuteReader () returns the DataReader object
ExecuteScalar () returns a single value, such as executing an SQL statement with Count (*) 4. Login Case public bool ValidateUser () {
BOOL Falg=true;
String constr = "Data source=.;i Nitial Catalog=schooldb; User=sa; Password=. ";
SqlConnection con = new SqlConnection (CONSTR);
Try
{
Open connection
Con. Open ();
Console.WriteLine ("Please enter user name:");
String Username=console.readline ();
Console.WriteLine ("Please enter password:");
string password = Console.ReadLine ();
1. Writing SQL
String sql = "SELECT COUNT (*) from Student where Studentname= '" +username+ "' and loginpwd= '" +password+ "'";
2. Create a Command object
SqlCommand com = new SqlCommand (Sql,con);
int count= (int) com. ExecuteScalar ();
if (Count > 0)
{
}
else {
Falg = false;
}
}
catch (Exception e)
{
Console.WriteLine (e); }
finally {
Con. Close ();
}
return falg;
}

13th. Accessing the database using ADO

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.