·您的位置: 首页 » 资源教程 » 编程开发 » ASP.NET » C#中数据库操作

C#中数据库操作

类别: ASP.NET教程  评论数:0 总得分:0
  /*******************************
 * 功能:数据库操作相关
 * 作者:FlashICP
 * 时间:2005-7-22
 * ******************************/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace moban
{
 public class data
 {
  protected static string con=\"server=[yourserver];database=[yourdata];user id=[your id];password=[youpassword]\";
  //数据库字符串
  protected System.Data.SqlClient.SqlConnection DataConnection;
  //数据库连接对象
  protected System.Data.SqlClient.SqlDataAdapter DataAdapter;
  //SqlDataAdapter对象
  protected System.Data.SqlClient.SqlCommand DataCommand;
  //SqlCommand对象
  protected System.Data.SqlClient.SqlCommandBuilder DataComBuilder;
  //SqlCommandBuilder对象
  protected System.Data.DataSet DataSet;
  //DataSet对象
  protected System.Data.DataRow DataRow;
  //DataRow对象
  protected System.Data.SqlClient.SqlDataReader DataReader=null;
  //构造函数
  public data()
  {
   DataConnection=DataConn();
   
  }
  //连接数据库参数
  public string Con
  {
   
   get{return con;}
   set{con= value;}
   
  }
  //数据库连接参数
  public SqlConnection DataConn()
  {
   DataConnection=new SqlConnection(Con); 
   return DataConnection;
   
  }
  //返回数据库连接对象
  public SqlDataReader DataQuery(string Sql)
  {
   Sql=Sql.Replace(\"--\",\"\");
   Sql=Sql.Replace(\";\",\"\");
   DataConnection=DataConn();
   DataConnection.Open();
   DataReader=null;
   DataCommand=new SqlCommand(Sql,DataConnection);
   DataReader=DataCommand.ExecuteReader();
   DataCommand.Dispose();
   return DataReader;
  }
  //返回DataSet
  public virtual DataSet DataSetQuery(string Sql,string TableName)
  {
   DataConnection=DataConn();
   DataAdapter=new SqlDataAdapter(Sql,DataConnection);
   DataSet=new DataSet();
   DataAdapter.Fill(DataSet,TableName);
   DataAdapter.Dispose();
   DataConnection.Close();
   DataConnection.Dispose();
   return DataSet;
  }
  //返回DataSet
  public DataSet DataSetQuery(DataSet myds,string TableName,string Sql)
  {
   DataConnection=DataConn();
   DataAdapter=new SqlDataAdapter(Sql,DataConnection);
   DataAdapter.Fill(myds,TableName);
   DataConnection.Close();
   DataConnection.Dispose();
   return DataSet;

  }
  //返回DataSet
  public DataSet DataSetQuery(string Sql,string TableName,int StarIndex,int Count)
  {
   DataConnection=DataConn();
   DataConnection.Open();
   DataAdapter=new SqlDataAdapter(Sql,DataConnection);
   DataSet=new DataSet();
   DataAdapter.Fill(DataSet,StarIndex,Count,TableName);
   DataConnection.Close();
   DataConnection.Dispose();
   return DataSet;
  }
  public void DataDo(string sqlstr)
  {  
   DataConnection=DataConn();
   SqlCommand mycom=DataConnection.CreateCommand();
   mycom.CommandText=sqlstr;
   DataConnection.Open();
   mycom.ExecuteNonQuery();
   DataConnection.Close();
  }
  //返回DataSet 开始行到结束行
  public bool EditData(string Sql)
  {
   DataConnection=DataConn();
   DataConnection.Open();
   DataCommand=new SqlCommand(Sql,DataConnection);
   try
   {
    DataCommand.ExecuteNonQuery();
    DataConnection.Close();
    return true;
   }
   catch(Exception ex)
   { 
    System.Console.Write(ex.Message.ToString());
    DataConnection.Close();
    return false;
   }
  }
  //执行成功返回真,否则返回假.
  public object ExecuteScalar(string Sql)
  {
   DataConnection=DataConn();
   DataConnection.Open();
   DataCommand=new SqlCommand(Sql,DataConnection);
   try
   {
    object back = DataCommand.ExecuteScalar();
    DataConnection.Close();
    return back;
   }
   catch(Exception ex)
   { 
    System.Console.Write(ex.Message.ToString());
    DataConnection.Close();
    return null;
   }
  }
  //返回一个值的查询语句
  public DataSet EditData(DataSet DataSet,SqlDataAdapter myda,string TableName)
  {
    
   
   DataComBuilder=new SqlCommandBuilder(myda);
   myda.UpdateCommand=DataComBuilder.GetUpdateCommand();
   myda.DeleteCommand=DataComBuilder.GetDeleteCommand();
   myda.InsertCommand=DataComBuilder.GetInsertCommand();
   myda.Update(DataSet,TableName);
   DataSet.AcceptChanges();
   this.CloseConnection();
   return DataSet;
  }
  //修改DataSet 并且修改数据库值存储过程
  public bool DataProcedure(string ProcedureName)
  {
   DataConnection=DataConn();
   DataConnection.Open();
   DataCommand=new SqlCommand();
   DataCommand.CommandText=ProcedureName;
   DataCommand.Connection=DataConnection;
   
     
   DataCommand.CommandType=System.Data.CommandType.StoredProcedure;
   try
   {
    DataCommand.ExecuteNonQuery();
    return true;
   }
   catch(Exception ex)
   { 
    System.Console.Write(ex.Message.ToString());
    return false;
   }
  }
  
  //无参数存储过程的执行 返回执行真假
  public void CloseConnection()
  {
   if(DataReader!=null)
   {
    DataReader.Close();
    DataReader=null;

   }
   DataConnection.Close();
   DataConnection.Dispose();
  }
  //关闭连接
 

 }
}

-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1