DataBase.cs
上传用户:chizxy
上传日期:2014-11-29
资源大小:407k
文件大小:9k
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Windows.Forms;
- using System.IO;
- namespace MKIms3
- {
- /// <summary>
- /// DataBase 的摘要说明。
- /// </summary>
- public class DataBase
- {
- private SqlConnection conn;
- //private SqlCommand mycmd;
- // private SqlDataAdapter da;
- /// <summary>
- /// 构造函数
- /// </summary>
- public DataBase()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- this.conn = CreateConn();
- }
- /// <summary>
- /// 通过读取系统目录下的授权文件经过解密得到包含连接信息的数组
- /// </summary>
- /// <returns>包含连接信息的数组</returns>
- private string[] RetrunConn()
- {
- string input=null;
- if (!File.Exists(Application.StartupPath+"\授权文件.dat"))
- {
- MessageBox.Show("请确认您正确设置了授权文件","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- return null;
- }
- else
- {
- try
- {
- StreamReader sr = File.OpenText(Application.StartupPath+"\授权文件.dat");
- input=sr.ReadLine();
- sr.Close();
- string[] conAry = code_en_de.get_char(input);
- return conAry;
- }
- catch(Exception e)
- {
- MessageBox.Show("系统错误!nn错误信息:"+e.Message.ToString()+"","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- return null;
- }
- }
- }
- /// <summary>
- /// 返回SQLSERVER数据库的连接字符串
- /// </summary>
- /// <param name="conAry">包含连接信息的数组</param>
- /// <returns></returns>
- private string RSqlConn()
- {
- string[] conAry =RetrunConn();
- if(conAry==null)
- {
- return null;
- }
- else
- {
- string SqlConn = "Data Source="+conAry[0]+";Initial Catalog="+conAry[1]+";User ID="+conAry[2]+";Password="+conAry[3];
- return SqlConn;
- }
- }
- /// <summary>
- /// 返回工作空间连接字符串
- /// </summary>
- /// <param name="conAry">包含连接信息的数组</param>
- /// <returns>有两个元素的数组,为打开工作空间的两个参数</returns>
- public string[] MapConn()
- {
- string[] conAry = RetrunConn();
- if(conAry==null)
- {
- return null;
- }
- else
- {
- string[] WorkSpaceConn = new string[2];
- WorkSpaceConn[1] = "UID ="+conAry[2]+";pwd ="+conAry[3];
- WorkSpaceConn[0] = "Provider = SQLOLEDB;Driver = SQL Server;SERVER = "+conAry[0]+";Database ="+conAry[1]+";Caption = 某矿信息管理系统;";//这个是工作空间名
- return WorkSpaceConn;
- }
- }
- /// <summary>
- /// 建立数据库连接对象
- /// </summary>
- /// <returns>返回一个数据库连接对象</returns>
- private SqlConnection CreateConn()
- {
- string constring = RSqlConn();
- if(constring==null)
- {
- return null;
- }
- else
- {
- SqlConnection myConn = new SqlConnection(constring);
- return myConn;
- }
- }
- /// <summary>
- /// 建立command对象
- /// </summary>
- /// <param name="storedProcName">存储过程名称</param>
- /// <param name="parameters">参数数组</param>
- /// <returns>SqlCommand</returns>
- private SqlCommand BuildQueryCommand(string storedProcName,IDataParameter[] parameters)
- {
- if(conn!=null)
- {
- SqlCommand command = new SqlCommand(storedProcName,conn);
- command.CommandType = CommandType.StoredProcedure;
- foreach(SqlParameter parameter in parameters)
- {
- command.Parameters.Add(parameter);
- }
- return command;
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 建立command对象 重载
- /// </summary>
- /// <returns>SqlCommand</returns>
- private SqlCommand BuildQueryCommand(string storedProcName)
- {
- if(conn!=null)
- {
- SqlCommand command = new SqlCommand(storedProcName,conn);
- command.CommandType = CommandType.StoredProcedure;
- return command;
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 返回只读的向前的记录集
- /// </summary>
- /// <param name="storedProcName">存储过程名称</param>
- /// <param name="parameters">存储过程的参数数组</param>
- /// <returns>SqlDataReader</returns>
-
- public SqlDataReader RunProcedure(string storedProcName,IDataParameter[] parameters)
- {
- //SqlConnection conn = CreateConn();
- if(conn!=null)
- {
- SqlDataReader returnReader; //= new SqlDataReader();
- try
- {
-
- conn.Open();
- SqlCommand command = BuildQueryCommand(storedProcName,parameters);
- command.CommandType = CommandType.StoredProcedure;
- returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
- return returnReader;
- }
- catch(SqlException e)
- {
- MessageBox.Show("系统错误!nn错误信息:"+e.Message.ToString()+"","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- conn.Close();
- return null;
- }
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 返回只读的向前的记录集 重载
- /// </summary>
- /// <param name="storedProcName">存储过程名</param>
- /// <returns>SqlDataReader</returns>
- public SqlDataReader RunProcedure(string storedProcName)
- {
- if(conn!=null)
- {
- SqlDataReader returnReader;
- try
- {
- conn.Open();
- SqlCommand command = BuildQueryCommand(storedProcName);
- command.CommandType = CommandType.StoredProcedure;
- returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
- return returnReader;
- }
- catch(SqlException e)
- {
- MessageBox.Show("系统错误!nn错误信息:"+e.Message.ToString()+"","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- return null;
- }
- }
- else
- {
- return null;
- }
-
- }
- /// <summary>
- /// 返回一个dataset用来更新数据
- /// </summary>
- /// <param name="storedProcName">存储过程名</param>
- /// <param name="parameters">存储过程参数</param>
- /// <returns>dataset</returns>
- public DataSet RunProcedure(string storedProcName,IDataParameter[] parameters,string tablename)
- {
- DataSet dataset = new DataSet();
- SqlDataAdapter SqlDA = new SqlDataAdapter();
- try
- {
- SqlDA.SelectCommand = BuildQueryCommand(storedProcName,parameters);
- SqlDA.Fill(dataset,tablename);
- return dataset;
- }
- catch(SqlException e)
- {
- MessageBox.Show("系统错误!nn错误信息:"+e.Message.ToString()+"","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- return null;
- }
- finally
- {
- conn.Close();
- }
- }
- /// <summary>
- /// 返回一个dataset用来更新数据 重载
- /// </summary>
- /// <param name="storedProcName">存储过程名</param>
- /// <returns>dataset</returns>
- public DataSet RunProcedure(string storedProcName,string tablename)
- {
- DataSet dataset = new DataSet();
- SqlDataAdapter SqlDA = new SqlDataAdapter();
- try
- {
- SqlDA.SelectCommand = BuildQueryCommand(storedProcName);
- SqlDA.Fill(dataset,tablename);
- return dataset;
-
- }
- catch(SqlException e)
- {
- MessageBox.Show("系统错误!nn错误信息:"+e.Message.ToString()+"","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- return null;
- }
- finally
- {
- conn.Close();
- }
- }
- /// <summary>
- /// 执行无返回值的存储过程
- /// </summary>
- /// <param name="storedProcName">存储过程名</param>
- /// <param name="parameters">存储过程的参数</param>
- /// <returns>执行是否成功</returns>
- public bool RunProcedure_Nr(string storedProcName,IDataParameter[] parameters)
- {
- try
- {
- conn.Open();
- SqlCommand mycommand = new SqlCommand();
- mycommand = BuildQueryCommand(storedProcName,parameters);
- mycommand.ExecuteNonQuery();
- return true;
- }
- catch(SqlException e)
- {
- MessageBox.Show("系统错误!nn错误信息:"+e.Message.ToString()+"","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- return false;
-
- }
- finally
- {
- conn.Close();
- }
- }
- public bool Run_change(string selectaa,DataSet myset,string tablename)
- {
- // string selectaa = " select ceng.煤层名称, ceng.止煤深度, ceng.底板坐标x, ceng.底板坐标y, ceng.底板坐标z, ceng.煤层伪厚, ceng.煤层倾角, ceng.利用厚度, ceng.顶板岩性, ceng.底板岩性, ceng.钻孔序号, ceng.等级 from 煤层关联钻孔 as ceng where 1>2";
-
- try
- {
- SqlDataAdapter da = new SqlDataAdapter(selectaa,conn);
- SqlCommandBuilder cd = new SqlCommandBuilder(da);
- da.Update(myset,tablename);
- return true;
- }
- catch(SqlException e)
- {
- MessageBox.Show("系统错误!nn错误信息:"+e.Message.ToString()+"","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- return false;
-
- }
- finally
- {
- conn.Close();
- }
- }
- // public DataSet get_DataSet(string select,string tablename)
- // {
- // //mycmd = new SqlCommand(select,this.conn);
- // da = new SqlDataAdapter(select,conn);
- // DataSet myset = new DataSet();
- // da.Fill(myset,tablename);
- // return myset;
- // }
- //
- // public bool update_DataSet(DataSet myset,string tablename)
- // {
- // try
- // {
- // //SqlDataAdapter da = new SqlDataAdapter(mycmd,conn);
- // SqlCommandBuilder cb = new SqlCommandBuilder(da);
- // da.Update(myset,tablename);
- // return true;
- // }
- // catch(SqlException e)
- // {
- // MessageBox.Show("系统错误!nn错误信息:"+e.Message.ToString()+"","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- // return false;
- //
- // }
- // finally
- // {
- // conn.Close();
- // }
- //
- // }
- }
- }