ADO.NET在C#中关于SQL操作的理解

来源:互联网 发布:中国软件学院 编辑:程序博客网 时间:2024/04/28 08:25

         SQL语言可以操作关系数据库,但在.NET中无法直接使用SQL语言。.NET提供了ADO.NET组件来访问和操作数据库,组建简单的来说就是一组想关的类(Connection  ,Command, DataReader, DataADapter ,DataSet.),访问和操作不同的数据库需要使用不同的数据提供程序,现在就一SQL Sever 数据库为例。一般的数据库操作无非就是读和写,ADO.NET中提供了只读的类DataReader和可读可写的类DataSet。

         DataReader 只能向前和向右读取数据,它类似一个标尺,如果当前读取数据的过程中连接中断,则数据丢失(当然数据库中的数据不会丢失)。当然这些大家都知道,但为什么是只能向前向右,而且数据在连接中断又连接后不能恢复呢 ?我个人理解是DataReader在内存当中通过一个类似指针的东西来连接到数据库中既读即取,这也就是为什么DataReader只能读而不能修改和写的原因了。

        DataSet类呢它是把数据一次取到内存当中存放在TABLE中(table可以有多个,具体要用那个表当中的可以这样来写DataSet   ds= new DataSet()  ;   ds.Table[i].Rows 来制定)存放在内存中是一个什么概念?这也就是为什么有权限的用户可以修改,读取,写入数据库了,因为内存是在客户端,数据在用户自己的控制范围之内,我想修改就修改,只要写入的时候符合约束,其他都ok。

  下面就贴一段简单的SQLHelper类中的代码:

// File:    SqlDbHelper.cs
// Author:  audrey
// Created: 2009年6月15日 10:30:03
// Purpose: Definition of Class SqlDbHelper

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;

public class SqlDbHelper
{
   //private static string CONNECTION_STRING = "server=william\\sqlexpress;database=musicbar;uid=sa;pwd=william.shan";
    //Data Source=.\SQLEXPRESS;AttachDbFilename=D:\MusicWebSite\ch6\App_Data\MusicBar.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
    private static string CONNECTION_STRING = @"Data Source=.;DataBase=MusicBar;Integrated Security=True";

    /// <summary>
    /// 执行Select语句,
    /// </summary>
    /// <param name="strSql"></param>
    /// <returns></returns>
    public static DataSet ExecuteSelectSql(string strSql)
   {
       SqlConnection conn = new SqlConnection(CONNECTION_STRING);
       SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
       DataSet ds = new DataSet();
       try
       {
           da.Fill(ds);
           return ds;
       }
       catch (Exception ex)
       {
           throw new Exception("执行SQL出现错误:\r\n"+strSql+"\r\n"+ex.ToString());
       }
       finally
       {
           conn.Close();
       }
       
       
      //throw new NotImplementedException();
   }
   /// <summary>
   /// 执行UPDATE语句
   /// </summary>
   /// <param name="strSql"></param>
   /// <returns></returns>
   public static int ExecuteUpdateSql(string strSql)
   {

       SqlConnection conn = new SqlConnection(CONNECTION_STRING);
       SqlCommand comm = new SqlCommand();
       comm.Connection = conn;
       comm.CommandType = CommandType.Text;
       comm.CommandText = strSql;
       int ret = -1;
       try
       {
           conn.Open();
           ret = comm.ExecuteNonQuery();

           return ret;
       }
       catch (Exception ex)
       {
           throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
       }
       finally
       {
           conn.Close();
       }
       
       //throw new NotImplementedException();
   }
   /// <summary>
   /// 执行Delete语句
   /// </summary>
   /// <param name="strSql"></param>
   /// <returns></returns>
    public static int ExecuteDelSql(string strSql)
   {
       SqlConnection conn = new SqlConnection(CONNECTION_STRING);
       SqlCommand comm = new SqlCommand(strSql,conn);       
       int ret = -1;
       try
       {
           conn.Open();
           ret = comm.ExecuteNonQuery();
           return ret;
       }
       catch (Exception ex)
       {
           throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
       }
       finally
       {
           conn.Close();
       }
       
      //throw new NotImplementedException();
   }
   /// <summary>
   /// 执行Insert语句
   /// </summary>
   /// <param name="strSql"></param>
   /// <returns></returns>
    public static int ExecuteInsertSql(string strSql)
   {
       SqlConnection conn = new SqlConnection(CONNECTION_STRING);
       SqlCommand comm = new SqlCommand();
       comm.Connection = conn;
       comm.CommandType = CommandType.Text;
       comm.CommandText = strSql;
       int ret = -1;
       try
       {
           conn.Open();
           ret = comm.ExecuteNonQuery();

           return ret;
       }
       catch (Exception ex)
       {
           throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
       }
       finally
       {
           conn.Close();
       }
       

      //throw new NotImplementedException();
   }
   
   public static Object ExecuteScalar(string strSql)
   {
       SqlConnection conn = new SqlConnection(CONNECTION_STRING);
       SqlCommand comm = new SqlCommand();
       comm.Connection = conn;
       comm.CommandType = CommandType.Text;
       comm.CommandText = strSql;
       
       try
       {
           conn.Open();
           SqlDataReader dr = comm.ExecuteReader();
           if (dr.Read())
           {
               return dr[0];
           }
           else
           {
               return null;
           }
       }
       catch (Exception ex)
       {
           throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
       }
       finally
       {
           conn.Close();
       }
       

      //throw new NotImplementedException();
   }
   
   public SqlConnection GetConnection()
   {
       SqlConnection conn = new SqlConnection(CONNECTION_STRING);
       return conn;
      //throw new NotImplementedException();
   }
}

    关于数据库的操作根据操作的不同选择不同的类,具体如下图:



   关于其它数据库的操作就不一一叙述了,当掌握了操作一门数据库的操作之后,举一反三,只要添加适当的类,就ok。

   水平有限,欢迎拍砖,大家互相交流学习嘛!!!


原创粉丝点击