c#连接sql2000

http://blog.csdn.net/qisir1981/archive/2006/07/28/994904.aspx

在控制台应用程序下连接SQL,数据库使用MSSQL自带的Northwind.

1. 创建sqlconnection类的连接对象
SqlConnection mySqlConnection =
new Sqlconnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
//server指定运行sql的计算机名,localhost是常用名表示程序所在的计算机

2.建立sqlcommand对象
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

3.设置SqlCommand对象的CommandText属性
mySqlCommand.CommandText =
"SELECT CustomerID,CompanyName,ContactName,Address" +
"FROM Customer" +
"WHERE CustomerID = 'ALFKI' ";

4.打开sqlconnection对象
mySqlConnection.Open();

5.运行SELECT语句
SqlDataReader mySqlDataReader = mySqlCommad.ExecuteReader();
//调用ExecuteReader()方法运行SELECT,返回一个SqlDataReader对象

6.读行
mySqlDataReader.Read();

7.显示SqlDataReader对象中的列值
Console.WriteLine("mySqlDataReader[/"CustomerId/"]="+
mySqlDataReader["CustomerID"]");
//以下依次是Commpanyname等项...

8.关闭Sqlconnection的连接
mySqlConnection.Close();

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qisir1981/archive/2006/07/28/994904.aspx

你可能感兴趣的:(sql2000)