对数据库的几种基本操作

对数据库的几种基本操作
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  Microsoft.Win32;
None.gif
using  System.Reflection;
None.gif
using  System.IO;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Runtime.InteropServices ;
None.gif
using  System.Text;
None.gif
namespace  IMS.Class
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// LinkDatabase 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class LinkDatabase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string strSQL="";
InBlock.gif        
//private string connectionString = "Data Source=WYQ;Persist Security Info=False;Initial Catalog=InSave;Integrated Security=SSPI";
InBlock.gif
        private SqlConnection myConnection;
InBlock.gif        
InBlock.gif        
private SqlCommandBuilder sqlCmdBld;
InBlock.gif        
private DataSet ds = new DataSet();
InBlock.gif        
private SqlDataAdapter da;
InBlock.gif
InBlock.gif        
public string DB_Conn="";
InBlock.gif        
public LinkDatabase()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif
            DB_Conn="Persist Security Info=False;Data Source=127.0.0.1;Initial Catalog=DelegateIMS;User ID=sa;Password=sa";//ConfigurationSettings.AppSettings["ConnStr"]; 
InBlock.gif        
//Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=DelegateIMS;Data Source=MICROSOF-9C75B7;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=ZHIYANG;Use Encryption for Data=False;Tag with column collation when possible=False
ExpandedSubBlockEnd.gif
        }
 
InBlock.gif        
public LinkDatabase(string Str) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
this.DB_Conn = Str; 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch(Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
throw ex; 
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public bool JudgeServer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.open();
InBlock.gif            
if(this.myConnection.State==ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.myConnection.Close();
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void open()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//调试时若无此句,将使得运行时提示:未将对象引用到实例
InBlock.gif
            this.myConnection=new SqlConnection(this.DB_Conn);
InBlock.gif            
if(this.myConnection.State==ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                
return ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.myConnection.Open();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(System.Data.SqlClient.SqlException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new Exception(""+ex.Message +"");
ExpandedSubBlockEnd.gif                }

InBlock.gif    
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///  根据输入的SQL语句检索数据库数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tempStrSQL">检索的sql语句</param>
InBlock.gif        
/// <param name="tempTableName">映射的表名</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataSet SelectDataBase(string tempStrSQL,string tempTableName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
this.strSQL = tempStrSQL;
InBlock.gif            
this.myConnection = new SqlConnection(DB_Conn);
InBlock.gif            
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
InBlock.gif            
this.ds.Clear();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.da.Fill(ds,tempTableName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
throw new Exception(""+e.Message +"");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///  数据库数据更新(传DataSet和DataTable的对象)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="changedDataSet">改变了的dataset</param>
InBlock.gif        
/// <param name="tableName">映射源表的表名</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回更新了的数据库表</returns>

InBlock.gif        public DataSet UpdateDataBase(DataSet changedDataSet,string tableName,string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.myConnection = new SqlConnection(DB_Conn);
InBlock.gif                
this.da = new SqlDataAdapter(str,this.myConnection);
InBlock.gif                  
this.da.SelectCommand=new SqlCommand(str,this.myConnection);
InBlock.gif                
this.sqlCmdBld = new SqlCommandBuilder(da);
InBlock.gif                
this.da.Update(changedDataSet,tableName);
InBlock.gif                
return changedDataSet;//返回更新了的数据库表
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif               
throw new Exception(""+ex.Message +"");
ExpandedSubBlockEnd.gif            }
                         
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//  直接操作数据库(未创建该类的实例时直接用)  /////
InBlock.gif
InBlock.gif        
/// <summary>
InBlock.gif        
///  检索数据库数据(传字符串,直接操作数据库)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tempStrSQL">检索的sql语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>查询的结果,存在于一个datatable中</returns>

InBlock.gif        public DataTable SelectDataBase(string tempStrSQL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.myConnection = new SqlConnection(DB_Conn);
InBlock.gif            DataSet tempDataSet 
= new DataSet();
InBlock.gif            
this.da = new SqlDataAdapter(tempStrSQL,this.myConnection);
InBlock.gif            
this.da.Fill(tempDataSet);
InBlock.gif            
return tempDataSet.Tables[0];
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///  数据库数据更新(传字符串,直接操作数据库)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tempStrSQL">检索的sql语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回数据库中影响的行数</returns>

InBlock.gif        public int UpdateDataBase(string tempStrSQL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.myConnection = new SqlConnection(DB_Conn);
InBlock.gif            
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
InBlock.gif
            myConnection.Open();
InBlock.gif            SqlCommand tempSqlCommand 
= new SqlCommand(tempStrSQL,this.myConnection);
InBlock.gif            
int intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
InBlock.gif
            return intNumber;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///  关闭数据库
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void CloseDataBase()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.myConnection.Close();
InBlock.gif            
this.myConnection.Dispose();
InBlock.gif            
this.ds.Clear();
InBlock.gif            
this.ds.Dispose();
InBlock.gif            GC.Collect(); 
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 返回SQL语句执行结果的第一行第一列 
InBlock.gif        
/// </summary> 
ExpandedSubBlockEnd.gif        
/// <returns>字符串</returns> 

InBlock.gif        public string ReturnValue(string SQL) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
this.myConnection = new SqlConnection(DB_Conn); 
InBlock.gif            myConnection.Open(); 
InBlock.gif            
string result; 
InBlock.gif            SqlDataReader Dr ; 
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                SqlCommand Cmd
= new SqlCommand(SQL,this.myConnection); 
InBlock.gif                Dr 
= Cmd.ExecuteReader(); 
InBlock.gif                
if (Dr.Read()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    result 
= Dr[0].ToString(); 
InBlock.gif                    Dr.Close(); 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    result 
= ""
InBlock.gif                    Dr.Close(); 
ExpandedSubBlockEnd.gif                }
 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
throw new Exception(SQL); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            Dispose(
this.myConnection); 
InBlock.gif            
return result; 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 运行存储过程,返回dataset. 
InBlock.gif        
/// </summary> 
InBlock.gif        
/// <param name="procName">存储过程名.</param> 
InBlock.gif        
/// <param name="prams">存储过程入参数组.</param> 
ExpandedSubBlockEnd.gif        
/// <returns>dataset对象.</returns> 

InBlock.gif        public DataSet RunProc(string procName,SqlParameter[] prams,DataSet Ds) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
this.myConnection = new SqlConnection(DB_Conn); 
InBlock.gif            myConnection.Open();
InBlock.gif            SqlCommand Cmd 
= new SqlCommand(procName, this.myConnection); 
InBlock.gif            Cmd.CommandType 
= CommandType.StoredProcedure; 
InBlock.gif            
if (prams != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
foreach (SqlParameter parameter in prams) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    
if(parameter != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        Cmd.Parameters.Add(parameter); 
ExpandedSubBlockEnd.gif                    }
 
ExpandedSubBlockEnd.gif                }
 
ExpandedSubBlockEnd.gif            }
  
InBlock.gif            SqlDataAdapter Da 
= new SqlDataAdapter(Cmd); 
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                Da.Fill(Ds); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch(Exception Ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
throw Ex; 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
return Ds; 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 返回SQL语句第一列,第ColumnI列, 
InBlock.gif        
/// </summary> 
ExpandedSubBlockEnd.gif        
/// <returns>字符串</returns> 

InBlock.gif        public string ReturnValue(string SQL, int ColumnI) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
this.myConnection = new SqlConnection(DB_Conn); 
InBlock.gif            myConnection.Open();
InBlock.gif            
string result; 
InBlock.gif            SqlDataReader Dr ; 
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                SqlCommand Cmd
= new SqlCommand(SQL,this.myConnection); 
InBlock.gif                Dr 
= Cmd.ExecuteReader(); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
throw new Exception(SQL); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
if (Dr.Read()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                result 
= Dr[ColumnI].ToString(); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                result 
= ""
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            Dr.Close(); 
InBlock.gif            Dispose(
this.myConnection); 
InBlock.gif            
return result; 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
public void Dispose(SqlConnection Conn) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
if(Conn!=null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                Conn.Close(); 
InBlock.gif                Conn.Dispose(); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            GC.Collect(); 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/oflying907/archive/2006/04/17/377282.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值