C # Connection Database SQL (2005)

Source: Internet
Author: User

The original Published time: 2008-07-24--from my Baidu article [imported by moving tools]

Finally took the road to the start.
First of all, to summarize the whole process of database programming, here is the SQL SERVER
(1) Establish SqlConnection objects to connect to the database
SqlConnection mysqlconnection=new SqlConnection ("server=.\\sqlexpress;
Database=yourdatabase;uid=sa;pwd=sa ");

(2) Setting up SqlCommand objects
SqlCommand mysqlcommand=mysqlconnection. CreateCommand ();

(3) Set the CommandText property of the SqlCommand object
mysqlcommand.commandtext= "SELECT * from Youydatabasetablename";

(4) Open SqlConnection object
Mysqlconnection.open ();

(5) Run the SELECT statement
SqlDataReader Mysqldatareader=mysqlcommand. ExecuteReader ();

(6) Read line with SqlDataReader object
Mysqldatareader. Read ();

(7) Display the values in the SqlDataReader object
Yourtextbox. text=mysqldatareader["ID"]. ToString ();
ID is a column name in your database

(8) Close SqlDataReader Object
Mysqldatareader. Close ();

(9) Close SqlConnection object
Mysqlconnection. Close ();

The process is presented below.

1] In my program is SQL, so in the program head Plus
Using System.Data.SqlClient;

2] Add the appropriate controls to the Form1 to display the data.

3] Application variables
static private int m = 1; Show each record
static private int k; Get the number of record strips
SqlCommand Mysqlcommand;
SqlConnection mysqlconnection;

4] Connect to the database, note the connection string here, with the security type.
Try
{
Mysqlconnection = new SqlConnection ("Server=localhost;
Integrated security=true;database=myfriends ");
Mysqlcommand = mysqlconnection. CreateCommand ();
Get the number of record strips
Mysqlcommand.commandtext = "Select COUNT (*) from Friends";
Mysqlconnection. Open ();
k = (int) Mysqlcommand. ExecuteScalar ();

GetData (1);
}
catch (SqlException ex)
{
MessageBox.Show ("A SqlException was Thrown,number =" +
Ex. Number +//error numbers
Ex. Message +//Error description string
Ex. StackTrace); Class name and method name string that throws an exception
}

5] If you have more than one record in the database, you should show it
In a button event that is displayed in an itemized display:
M + = 1;//Next index value
if (M > k)
{
MessageBox.Show ("The last one!") The first record will be displayed! "," information tip ");
m = 1;
}
This.hisname.Text = ""; To display the next bar, of course, first empty
This.hissex.Text = "";
This.hisadress.Text = "";
This.hisbirthday.Text = "";

GetData (m);//Display the next data

6] Displays the next function in the display data;
private void GetData (int i)
{
            Mysqlcommand. CommandText = "Select Id,name,sex," +
                             "Birthday,address From Friends WHERE id= "+i;
            SqlDataReader Mysqldatareader = Mysqlcommand. ExecuteReader ();
            Mysqldatareader. Read ();

This.hisname.Text = mysqldatareader["Name"]. ToString ();
This.hissex.Text = mysqldatareader["Sex"]. ToString ();
This.hisadress.Text = mysqldatareader["Address"]. ToString ();
This.hisbirthday.Text = mysqldatareader["Birthday"]. ToString ();

Mysqldatareader. Close ();
}

I hope you have some help in reading!
Also to encourage themselves to continue to learn!

C # Connection Database SQL (2005)

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.