当前位置:编程学习 > asp >>

ADO.NET数据访问技术入门

ADO.NET访问SQL Server示例

先在Microsoft SQL Server Management Studio中测试一下该T-SQL语句。后面的示例中将以此T-SQL语句来讲解。

select * from employees where employeeid between 2 and 6  

查询结果如下:

那么在利用C#构建应用程序时,如何在代码中完成数据访问功能呢。请看示例代码:

示例代码A:

using System;

using System.Data;

using System.Data.SqlClient;


namespace SqlServerProvider

{

    class Program

    {

        static void Main(string[] args)

        {

            // 设置连接字符串

            string connString = @"

            Server =.;

            Integrated Security=True;

            Initial Catalog = Northwind

         ";

            // 设置查询字符串,硬编码方式

            string sqlqry = @"select * from employees where employeeid between 2 and 6";

            // 声明数据连接和数据读取器对象变量

            SqlConnection conn = null;

            SqlDataReader reader = null;

            try

            {

                conn = new SqlConnection(connString);

                SqlCommand cmdqry = conn.CreateCommand();

                cmdqry.CommandType = CommandType.Text;

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,