对数据库的几种基本操作

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

    public class LinkDatabase
    
dot.gif{
        
private string strSQL="";
        
//private string connectionString = "Data Source=WYQ;Persist Security Info=False;Initial Catalog=InSave;Integrated Security=SSPI";
        private SqlConnection myConnection;
        
        
private SqlCommandBuilder sqlCmdBld;
        
private DataSet ds = new DataSet();
        
private SqlDataAdapter da;

        
public string DB_Conn="";
        
public LinkDatabase()
        
dot.gif{
            
// TODO: 在此处添加构造函数逻辑
            DB_Conn="Persist Security Info=False;Data Source=127.0.0.1;Initial Catalog=DelegateIMS;User ID=sa;Password=sa";//ConfigurationSettings.AppSettings["ConnStr"]; 
        
//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
        }
 
        
public LinkDatabase(string Str) 
        
dot.gif
            
try 
            
dot.gif
                
this.DB_Conn = Str; 
            }
 
            
catch(Exception ex) 
            
dot.gif
                
throw ex; 
            }
 
        }

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

            
else
                
return false;
        }

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

            
else
                
try
                
dot.gif{
                    
this.myConnection.Open();
                }

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

    
        }

        
        
/**//// 
        
///  根据输入的SQL语句检索数据库数据
        
/// 

        
/// 检索的sql语句
        
/// 映射的表名
        
/// 

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

            
catch(Exception e)
            
dot.gif
                
throw new Exception(""+e.Message +"");
            }

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


        
/**//// 
        
///  数据库数据更新(传DataSet和DataTable的对象)
        
/// 

        
/// 改变了的dataset
        
/// 映射源表的表名
        
/// 返回更新了的数据库表

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

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

        
/**//  直接操作数据库(未创建该类的实例时直接用)  /////

        
/// 
        
///  检索数据库数据(传字符串,直接操作数据库)
        
/// 

        
/// 检索的sql语句
        
/// 查询的结果,存在于一个datatable中

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


        
/**//// 
        
///  数据库数据更新(传字符串,直接操作数据库)
        
/// 

        
/// 检索的sql语句
        
/// 返回数据库中影响的行数

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


        
/**//// 
        
///  关闭数据库
        
/// 

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

        
/**//// 返回SQL语句执行结果的第一行第一列 
        
///  
        
/// 字符串 

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

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

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

}

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

你可能感兴趣的:(数据库,runtime,ui)