Hướng dẫn kết nối Database SQLServer2008 với ADO.NET

Hướng dẫn kết nối Database SQLServer2008 với ADO.NET

Bước 1: Tạo ứng dụng Windows Application.  

Tạo Form đăng ký để insert dữ liệu. Mở SQL Server 2008, sau đó tạo database và table.

Chú ý: Trong form phải using namespace System.Data.SqlClient để có thể kết nối với CSDL SQLServer 2008.

Bước 2: Tạo đối tượng SqlConnection để kết nối với CSDL

SqlConnection connection = new SqlConnection("server=CUONGNH;database=HungCuong;uid=sa;pwd=123456";

connection.Open();

Giải thích các tham số trong chuỗi kết nối:
server: Tên của sql server instance.

Database: Tên của database.

Uid: User id đăng nhập vào SQL Server.

Pwd: Password của user đăng nhập vào SQL Server.

Bước 3: Tạo đối tượng SQLCommand

//tao ra doi tuong command

SqlCommand command = connection.CreateCommand();

command.CommandText = "Insert into BookMaster values('" + txtTitle.Text + "','" +               cboLanguage.SelectedItem.ToString() + "','" + cboAuthor.SelectedItem.ToString() + "'," + updPageNumber.Value + ", " + Convert.ToInt32(txtPrice.Text) + ")";

Bước 4: Cập nhật dữ liệu

//thuc hien update du lieu

int result = command.ExecuteNonQuery();

if (result >= 1)               

MessageBox.Show("Bạn đã đăng ký sách thành công !");

Bước 5: Đóng kết nối

//close connection

connection.Close();

Bước 6: Chạy chương trình để xem kết quả

Hướng dẫn kết nối Database SQLServer2008 với ADO.NET

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Editing, Sharing Photos Becomes Easier On Android Phones Through Adobe Photoshop

Editing, Sharing Photos Becomes Easier On Android Phones Through Adobe Photoshop Express

There are tons of free photo editing apps provided by the Android Market.

Khắc phục hỏng hóc máy in canon, epson

Khắc phục hỏng hóc máy in canon, epson

Ai cũng có lúc ghét máy in. Khi bạn cần in nhất thì lại có một bảng báo lỗi ngớ ngẩn, hay làm bạn rối tung vì bị kẹt giấy hoặc lem mực.

Hướng dẫn tạo blog trong Symfony 2 framework

Hướng dẫn tạo blog trong Symfony 2 framework

This tutorial will guide you through the process of creating a full featured blogging website using Symfony2. The Standard Distribution of the Symfony2 framework will be used

Công ty diệt chuột T&C

 

Diet con trung