C# 连接数据库……sqlserver
来源:互联网 发布:淘宝如何查拍a发b 编辑:程序博客网 时间:2024/06/06 01:07
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace TestC
{
/// <summary>
/// DBAccess の概要の説明です。
/// </summary>
public class DBAccess
{
protected SqlConnection Conn;
protected DataSet DS;
protected SqlDataAdapter SDA;
protected SqlCommand SC;
protected SqlDataReader SDR;
public DBAccess()
{
//
// TODO: コンストラクタ ロジックをここに追加してください。
//
}
public void Open()
{
try
{
//string ConnStr="";
Conn=new SqlConnection(@"server=10.69.0.3/BKUPEXEC;uid=ntt;pwd=ntt;database=CallCenter");
//Conn=new SqlConnection(ConnStr);
Conn.Open();
}
catch(Exception e)
{
WriteMessage(e.Message.ToString(),true,true);
}
}
public void Close()
{
try
{
// System.Data.ConnectionState
//System.Data.ConnectionState
if(Conn.State ==ConnectionState.Open )
Conn.Close();
}
catch(Exception e)
{
WriteMessage(e.Message.ToString(),true,true);
}
}
public void WriteMessage(string strMsg,bool Back,bool End)
{
MessageBox.Show(strMsg);
}
public void Dispose()
{
if(DS!=null)
DS.Dispose();
if(SDA!=null)
SDA.Dispose();
if(SC!=null)
SC.Dispose();
if(SDR!=null)
{
if(!SDR.IsClosed)
SDR.Close();
}
}
public void Fill(string strSql)
{
try
{
Open();
SDA=new SqlDataAdapter(strSql,Conn);
DS=new DataSet();
SDA.Fill(DS);
}
catch(Exception e)
{
WriteMessage(e.Message.ToString().Trim(),true,true);
}
finally
{
Close();
Dispose();
}
}
public void Fill(string TableName,string strSql)
{
try
{
Open();
SDA=new SqlDataAdapter(strSql,Conn);
DS=new DataSet();
SDA.Fill(DS,TableName);
}
catch(Exception e)
{
WriteMessage(e.Message.ToString().Trim(),true,true);
}
finally
{
Close();
Dispose();
}
}
public DataSet getDataOut()
{
return DS;
}
//get one row
public DataRow GetRecord(string strSql)
{
DataRow DR=null;
try
{
Open();
SDA=new SqlDataAdapter(strSql,Conn);
DS=new DataSet();
SDA.Fill(DS);
if(DS.Tables[0].Rows.Count>0)
{
DR=DS.Tables[0].Rows[0];
}
return DR;
}
catch(SqlException e)
{
WriteMessage(e.Message.ToString(),true,true);
return DR;
}
finally
{
Close();
Dispose();
}
}
//get row count
public int GetRowCount(string strSql)
{
Fill(strSql);
try
{
int count=DS.Tables[0].Rows.Count;
return count;
}
catch
{
return 0;
}
finally
{
Close();
Dispose();
}
}
}
}
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace TestC
{
/// <summary>
/// DBAccess の概要の説明です。
/// </summary>
public class DBAccess
{
protected SqlConnection Conn;
protected DataSet DS;
protected SqlDataAdapter SDA;
protected SqlCommand SC;
protected SqlDataReader SDR;
public DBAccess()
{
//
// TODO: コンストラクタ ロジックをここに追加してください。
//
}
public void Open()
{
try
{
//string ConnStr="";
Conn=new SqlConnection(@"server=10.69.0.3/BKUPEXEC;uid=ntt;pwd=ntt;database=CallCenter");
//Conn=new SqlConnection(ConnStr);
Conn.Open();
}
catch(Exception e)
{
WriteMessage(e.Message.ToString(),true,true);
}
}
public void Close()
{
try
{
// System.Data.ConnectionState
//System.Data.ConnectionState
if(Conn.State ==ConnectionState.Open )
Conn.Close();
}
catch(Exception e)
{
WriteMessage(e.Message.ToString(),true,true);
}
}
public void WriteMessage(string strMsg,bool Back,bool End)
{
MessageBox.Show(strMsg);
}
public void Dispose()
{
if(DS!=null)
DS.Dispose();
if(SDA!=null)
SDA.Dispose();
if(SC!=null)
SC.Dispose();
if(SDR!=null)
{
if(!SDR.IsClosed)
SDR.Close();
}
}
public void Fill(string strSql)
{
try
{
Open();
SDA=new SqlDataAdapter(strSql,Conn);
DS=new DataSet();
SDA.Fill(DS);
}
catch(Exception e)
{
WriteMessage(e.Message.ToString().Trim(),true,true);
}
finally
{
Close();
Dispose();
}
}
public void Fill(string TableName,string strSql)
{
try
{
Open();
SDA=new SqlDataAdapter(strSql,Conn);
DS=new DataSet();
SDA.Fill(DS,TableName);
}
catch(Exception e)
{
WriteMessage(e.Message.ToString().Trim(),true,true);
}
finally
{
Close();
Dispose();
}
}
public DataSet getDataOut()
{
return DS;
}
//get one row
public DataRow GetRecord(string strSql)
{
DataRow DR=null;
try
{
Open();
SDA=new SqlDataAdapter(strSql,Conn);
DS=new DataSet();
SDA.Fill(DS);
if(DS.Tables[0].Rows.Count>0)
{
DR=DS.Tables[0].Rows[0];
}
return DR;
}
catch(SqlException e)
{
WriteMessage(e.Message.ToString(),true,true);
return DR;
}
finally
{
Close();
Dispose();
}
}
//get row count
public int GetRowCount(string strSql)
{
Fill(strSql);
try
{
int count=DS.Tables[0].Rows.Count;
return count;
}
catch
{
return 0;
}
finally
{
Close();
Dispose();
}
}
}
}
- C# 连接数据库……sqlserver
- C#连接sqlserver数据库
- c#连接sqlserver数据库
- c#连接sqlserver数据库
- c#连接sqlserver数据库
- c#连接sqlserver数据库
- c#连接sqlserver数据库
- C#连接SQLServer数据库
- 用 C# 连接SQLSERVER 数据库
- C#程序连接SqlServer数据库
- C# 连接SQLSERVER数据库 连接字符串
- JDBC 连接SQLServer数据库(F…
- ASP.NET C# 连接 access sqlserver 数据库
- C#连接SQLServer数据库关键代码
- c#连接sqlserver数据库并显示
- C# 连接、操作SqlServer数据库--日常笔记
- C#连接Access数据库,C#连接Excel数据库,C#连接SqlServer数据库,C#连接Mysql数据库总结
- 用ASP.NET/C#连接Access和SQLServer数据库
- 窗口类
- 批处理文件bat 语法备忘
- 看过的IBM网站
- SQL Server2000 各个版本之间的区别
- 寂静的时代即将来临
- C# 连接数据库……sqlserver
- 意外操作暴露Python的缺陷
- 2007年7月25日 Database开发小记(昨天)
- 从面试题看知识点
- 上期所1.7亿巨资上交易系统 是未雨绸缪还是超前投资
- BCB体验
- const几点用法
- 改善sql loader的性能
- 计算机英语天天学(07-07-24)