ADO.NET在C#中关于SQL操作的理解
2012-11-08 11:47
483 查看
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();
}
}
关于数据库的操作根据操作的不同选择不同的类,具体如下图:
![](http://img.my.csdn.net/uploads/201211/08/1352347746_9974.png)
![](http://img.my.csdn.net/uploads/201211/08/1352347775_7835.png)
关于其它数据库的操作就不一一叙述了,当掌握了操作一门数据库的操作之后,举一反三,只要添加适当的类,就ok。
水平有限,欢迎拍砖,大家互相交流学习嘛!!!
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();
}
}
关于数据库的操作根据操作的不同选择不同的类,具体如下图:
![](http://img.my.csdn.net/uploads/201211/08/1352347746_9974.png)
![](http://img.my.csdn.net/uploads/201211/08/1352347775_7835.png)
关于其它数据库的操作就不一一叙述了,当掌握了操作一门数据库的操作之后,举一反三,只要添加适当的类,就ok。
水平有限,欢迎拍砖,大家互相交流学习嘛!!!
相关文章推荐
- [置顶] ADO.NET在C#中关于SQL操作的理解
- 关于C# Ado.NET连接数据库操作基本实例模板
- ADO.NET下的SqlBulkCopy类执行数据库间批量复制操作
- 2.C#实验五:ADO.NET数据库操作练习详解
- 对C#中ADO.NET开发的理解
- C#操作数据库,DataSet,DataGridView,更新数据库 [一] - ADO.NET入门之中
- 用C#对ADO.NET数据库完成简单操作
- C#ADO.NET操作数据代码汇总
- C#里ADO.NET的SqlDataAdapter新增数据
- 数据库操作源码精选---Asp.net---ADO.net---C#
- 读写分离子系统 - C# SQL分发子系统(目前只支持ADO.NET)
- C# ADO.NET之SQL Server数据库操作
- C#ADO.NET操作数据代码汇总
- SQL基本操作 查询 排序 函数 ADO.net
- C#操作sql server数据库 ADO.NET
- 第二十九讲 : ADO.NET(玩SQL语句) 实例补充(OLEDB操作)
- 一个简单的ADO.NET C# 数据库操作实例
- ADO.NET操作数据代码汇总(C#)
- C# ADO.NET数据库操作
- 关于一些时间的操作 C# or SQL