using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace DataLibrary
{
///
/// 数据连接设置
///
public class SqlConnect
{
static string sqlconnstr;
///
/// 连接字符串
///
public string ConnString
{
set
{
sqlconnstr=value;
}
get
{
return sqlconnstr;
}
}
///
/// 取得连接
///
///
public SqlConnection GetSqlConnection()
{
SqlConnection _connection=null;
if(sqlconnstr==null || sqlconnstr=="")
{
string SystemDir=System.Environment.SystemDirectory;
DisposalConductService DisposalConductService1= new DisposalConductService(SystemDir+"\\CBERP_Dis.DLL") ;
sqlconnstr = "server="+DisposalConductService1.GetValue("ServerName","(local)")
+";user id="+DisposalConductService1.GetValue("SqlAccount","sa")
+";password="+DisposalConductService1.GetValue("PassWord","jiayu520")
+";Database="+DisposalConductService1.GetValue("Database","CBERP");
}
try
{
_connection = new SqlConnection(sqlconnstr);
_connection.Open();
}
catch(SqlException SE)
{
if(SE.Number==17 || SE.Number==18456)
{
if( MessageBox.Show("数据库访问参数设置不正确,是否对需要对其进行设置?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
sqlconnstr="";
DisposalConduct config = new DisposalConduct();
config.ShowDialog();
_connection=GetSqlConnection();
}
else
{
MessageBox.Show("建立数据库连接失败,程序不能继续运行!","错误",MessageBoxButtons.OK,MessageBoxIcon.Stop);
Application.ExitThread();
Application.Exit();
}
}
else
{
MessageBox.Show(SE.Message,"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
_connection.Close();
}
}
return _connection;
}
}
///
/// 数据配置
///
public class DataCatena : IDisposable
{
private SqlConnection _connection;
///
/// 数据配置
///
public DataCatena()
{
SqlConnect sqlconnect = new SqlConnect();
_connection = sqlconnect.GetSqlConnection();
_connection.Close();
}
///
/// 数据配置
///
/// 连接字符串
public DataCatena(String connStr)
{
_connection = new SqlConnection(connStr);
}
///
/// 取得DataView
///
/// 连接字符串
/// 数据视图
public DataView GetDataView(String Sqlstr)
{
DataView dv=null;
SqlDataAdapter myCommand = new SqlDataAdapter(Sqlstr, _connection);
DataSet ds = new DataSet();
try
{
myCommand.Fill(ds,"table");
dv=ds.Tables["table"].DefaultView;
}
catch(SqlException exc)
{
DebugBox db = new DebugBox(Sqlstr + "\r\n" + exc.Message);
db.Show();
}
return dv;
}
///
/// 清理资源
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
///
/// 清理所有正在使用的资源。
///
///
protected virtual void Dispose(bool disposing)
{
if (! disposing)
return;
if (this._connection != null)
{
this._connection.Dispose();
}
}
///
/// 取得数据适配器
///
/// SQL语句
/// 数据适配器
public SqlDataAdapter GetSqlDA(string Sqlstr)
{
SqlDataAdapter myCommand = new SqlDataAdapter(Sqlstr, _connection);
return myCommand;
}
///
/// 取得数据表
///
/// SQL语句
/// 数据表
public DataTable GetDT(String Sqlstr)
{
SqlDataAdapter myCommand = new SqlDataAdapter(Sqlstr, _connection);
DataSet ds = new DataSet();
try
{
myCommand.Fill(ds,"table");
}
catch(SqlException exc)
{
DebugBox db = new DebugBox(Sqlstr + "\r\n" + exc.ToString());
db.Show();
}
return ds.Tables["table"];
}
///
/// 执行语句
///
/// SQL语句
///
public object ExecWithValue(String Sqlstr)
{
object result=null;
_connection.Open();
SqlCommand myCommand = new SqlCommand(Sqlstr, _connection);
try
{
result=myCommand.ExecuteScalar();
}
catch (SqlException exc)
{
DebugBox db = new DebugBox(Sqlstr + "\r\n" + exc.ToString());
db.Show();
//MessageBox.Show(exc.Message);
}
_connection.Close();
return result;
}
///
/// 返回相应的行数
///
/// SQL语句
/// 行数
public int Exec(String Sqlstr)
{
int sucess=0;
SqlCommand myCommand = new SqlCommand(Sqlstr, _connection);
_connection.Open();
try
{
myCommand.ExecuteNonQuery();
}
catch (SqlException exc)
{
DebugBox db = new DebugBox(Sqlstr + "\r\n" + exc.ToString());
db.Show();
//
MessageBox.Show(exc.Message);
sucess=exc.Number;
}
_connection.Close();
return sucess;
}
}
///
/// 单表操作
///
public class SingleTable
{
private DataCatena DataCatena;
private SqlDataAdapter myDataAdapter;
private DataSet ds;
///
/// 单表操作
///
///
public SingleTable(string Sqlstr)
{
DataCatena = new DataCatena();
myDataAdapter = DataCatena.GetSqlDA(Sqlstr);
ds = new DataSet();
myDataAdapter.Fill(ds, "Table1");
}
///
/// 更新一个表
///
///
public void SetUpdateCommand(string Sqlstr)
{
myDataAdapter.UpdateCommand = new SqlCommand(Sqlstr,myDataAdapter.SelectCommand.Connection);
}
///
/// 返回数据表
///
///
public DataTable GetDataTable()
{
return ds.Tables["Table1"];
}
///
/// 返回数据适配器
///
///