📄 connectandaccessingdb.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net;
namespace ConnectDBLibrary
{
/// <summary>
/// 连接数据库并提供访问数据库的方法
/// </summary>
public class ConnectAndAccessingDB:IDisposable
{
SqlConnection connect;
SqlDataAdapter dataAdapter;
string ConnectionString="workstation id=\""+Dns.GetHostName()+"\";integrated security=SSPI;data source=\"" +
".\";persist security info=False;Initial Catalog=GroggeryDB";
public ConnectAndAccessingDB()
{
//string saConnectionString="server=.;database=GroggeryDB;uid=sa;pwd=aptech";
this.connect=new SqlConnection(ConnectionString);
}
/// <summary>
/// 双向操作
/// </summary>
/// <param name="cmdText">查询语句</param>
/// <param name="dataset">数据集</param>
public void Select(string cmdText,DataTable datatable )
{
try
{
this.dataAdapter = new SqlDataAdapter(cmdText,this.connect);
this.dataAdapter.Fill(datatable);
}
catch(Exception ex)
{
throw new ApplicationException("数据库操作失败!(双向)"+cmdText,ex);
}
finally
{
if(this.connect!=null)
this.connect.Close();
}
}
/// <summary>
/// 单向操作
/// </summary>
/// <param name="cmdText">更新语句</param>
/// <returns></returns>
public bool Update(string cmdText)
{
bool bl=false;
try
{
SqlCommand command = new SqlCommand(cmdText,this.connect);
this.connect.Open();
if(command.ExecuteNonQuery()>0)
{
bl=true;
}
}
catch(Exception ex)
{
throw new ApplicationException("数据库操作失败!(单向)",ex);
}
finally
{
if(this.connect!=null)
this.connect.Close();
}
return bl;
}
#region IDisposable 成员
public void Dispose()
{
if(this.connect!=null)
this.connect.Dispose();
if(this.dataAdapter!=null)
this.dataAdapter.Dispose();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -