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

 
Chat with Facebook friends through Go! Chat app for Androids

Chat with Facebook friends through Go! Chat app for Androids

Now that you got yourself an Android phone and you absolutely love it, you should go a head and make use of all the great apps that come with it.

Chỉ mất 15 phút để làm Drupal integration test

Chỉ mất 15 phút để làm Drupal integration test

This post will help you write and run your first Drupal integration test using Red Test framework in less than 15 minutes

Lịch sử module Drupal Statistics, thống kê truy cập

Lịch sử module Drupal Statistics, thống kê truy cập

Admittedly this was after in 2011 I tried to get the module removed from Drupal core.

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

 

Diet con trung