使用SQL Server提供者的简单控制台应用程序

数据提供者完成与数据源的实际连接。
.NET Framework把相关的类(经常在各种应用程序中使用)组织成为命名空间。
属于特定数据提供者的每个类都组织在不同的名称下,一起用于具体的命名空间中。

目前可用的数据提供者:
System.Data.SqlClient为SQL Server构成.NET数据提供者的类,专门为SQL Server 7.0和以上版本进行为优化。
System.Data.OleDb为兼容OLEDB数据源构成.NET数据提供者的类。
Microsoft.Data.Odbc用于直接访问Microsoft Open Database Connectivity(ODBC)驱动程序。
System.Data.OracleClient

以下是使用SqlServer提供者的简单例子,但还是先了解一下SqlClient命名空间中的一些类。
SqlCommand用于执行Transact-SQL语句或者存储过程。
SqlConnection代表与SQL Server数据源的连接。
SqlDataAdapter代表DataSet和数据源之间的关系。
SqlDataReader提供只向前移动的游标,用于读取SQL Server数据源。
SqlError保存有关SQL Server返回的错误和警告的信息。
SqlParameter代表SqlCommand的参数。
SqlTransaction代表对SQL Server数据源执行的Transact-SQL事务。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//导入命名空间,使能够访问这些命名空间的类
using System.Data.SqlClient;

namespace SQLServerProvider
{
    class Program
    {
        static void Main(string[] args)
        {
            //传递字符串,由服务器名称,安全授权信息和数据庫名组成
             string Constr=
                 "Server=localhost;Database=Northwind;Integrated security=true";//同Integrated security=SSPI
             SqlConnection sqlConn = new SqlConnection(Constr);

             try
             {
                
                 sqlConn.Open();//打开连接

                 string SQL = "SELECT * FROM Employees";
                 SqlCommand sqlCmm = new SqlCommand(SQL, sqlConn);
                 //sqlReader是抽象类,不能显示实例化它的对象。只能通过调用 
                 //sqlCommand的ExecuteReader()方法实例化,这个方法返回SqlDataReader实例
                 SqlDataReader sqlReader = sqlCmm.ExecuteReader();
                 //显示相关信息
                 Console.WriteLine("This program demonstrates the use of" +
                     "SQL Server.Net Data Provider");
                 Console.WriteLine("Querying database'{0}' with query"
                     + "'{1}'/n", sqlConn.Database, sqlCmm.CommandText);
                 //sqlDataReader类的Read()方法把DataReader移到下一条记录,
                 //如果有多行,返回true,否则,返回false
                 while (sqlReader.Read())
                 {
                     Console.WriteLine("{0}|{1}",
                         sqlReader["FirstName"].ToString().PadLeft(10),
                         sqlReader["LastName"].ToString().PadLeft(10));
                 }

             }
             catch (Exception ex)
             {
                 Console.WriteLine("Error:" + ex.Message);
             }
             finally //即使在try中关闭连接
             {
                 sqlConn.Close();
                 Console.ReadLine();
             }
            
        }
    }
}

OLE DB.NET数据提供者用于处理任何兼容OLE DB的数据仓库。
使用OLEDB数据提供者时,只需要稍做修改:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb; //命名空间做了修改

namespace SQLServerProvider
{
    class Program
    {
        static void Main(string[] args)
        {
            string strQuery = "Select * from Employees";
            string Constr =
               "Provider=SQLOLEDB;Database=Northwind;Integrated Security=SSPI"; //连接字符串做了修改
            OleDbConnection oledbConnection = new OleDbConnection(Constr);
            try
            {
                oledbConnection.Open();
                OleDbCommand oledbCommand = new OleDbCommand(strQuery, oledbConnection); //修改处
                OleDbDataReader dataReader = oledbCommand.ExecuteReader();
                while (dataReader.Read())
                {
                    Console.WriteLine("{0}{1}",
                        dataReader["FirstName"].ToString().PadRight(10),
                        dataReader["LastName"].ToString().PadRight(10));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:", e.Message);
            }
            finally
            {
                oledbConnection.Close();
                Console.ReadLine();
            }


        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值