.net 数据库操作
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DB
{
public class DataAccess
{
public string connectstring = System.Configuration.ConfigurationSettings.AppSettings["MyConnectionString"].ToString();
//public string connectstring = ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString();
private SqlConnection cn;
private SqlDataAdapter sda;
private SqlDataReader sdr;
private SqlCommand cmd;
private DataSet ds;
private DataView dv;
/// <summary>
/// 打开数据库连接
/// </summary>
public void Open()
{
#region
cn = new SqlConnection(connectstring);
cn.Open();
#endregion
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
#region
if (cn != null)
{
cn.Close();
cn.Dispose();
}
#endregion
}
/// <summary>
/// 返回DataSet数据集 错误返回null
/// </summary>
/// <param name="strSql">SQL语句</param>
public DataSet GetDs(string strSql)
{
#region
Open();
try
{
sda = new SqlDataAdapter(strSql, cn);
ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch
{
return null;
}
finally {
Close();
}
#endregion
}
/// <summary>
/// 添加DataSet表 无返回
/// </summary>
/// <param name="ds">DataSet对象</param>
/// <param name="strSql">Sql语句</param>
/// <param name="strTableName">表名</param>
public void GetDs(DataSet ds, string strSql, string strTableName)
{
#region
Open();
try
{
sda = new SqlDataAdapter(strSql, cn);
sda.Fill(ds, strTableName);
}
catch {
}
Close();
#endregion
}
/// <summary>
/// 返回DataView数据视图
/// </summary>
/// <param name="strSql">Sql语句</param>
public DataView GetDv(string strSql)
{
#region
dv = GetDs(strSql).Tables[0].DefaultView;
return dv;
#endregion
}
/// <summary>
/// 获得DataTable对象 错误返回空
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <returns></returns>
public DataTable GetTable(string strSql)
{
#region
try
{
return GetDs(strSql).Tables[0];
}
catch {
return null;
}
#endregion
}
/// <summary>
/// 获得一行数据(SqlDataReader) 出错抛出错误
/// </summary>
/// <param name="strSql">sql语句</param>
/// <returns></returns>
public SqlDataReader GetDataReader(string strSql)
{
#region
Open();
try{
cmd = new SqlCommand(strSql, cn);
sdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return sdr;
}
catch (Exception ex){
throw (ex);
}
finally {
Close();
}
#endregion
}
/// <summary>
/// 返回DataAdapter 出错抛出错误
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public SqlDataAdapter GetDataAdapter(string strSql)
{
#region
Open();
try
{
return new SqlDataAdapter(strSql, cn);
}
catch (Exception ex)
{
throw (ex);
}
finally {
Close();
}
#endregion
}
/// <summary>
/// 获得单个记录(object) 出错抛出错误
/// </summary>
/// <param name="strSql">sql语句</param>
/// <returns></returns>
public object GetSingeData(string strSql)
{
#region
Open();
try
{
cmd = new SqlCommand(strSql, cn);
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw (ex);
}
finally
{
Close();
}
#endregion
}
/// <summary>
/// 执行Sql语句 无返回值
/// </summary>
/// <param name="strSql"></param>
public void RunSql(string strSql)
{
#region
Open();
try
{
cmd = new SqlCommand(strSql, cn);
cmd.ExecuteNonQuery();
}
catch
{
}
finally {
Close();
}
#endregion
}
/// <summary>
/// 执行SQL语句,并返回第一行第一列结果的字符串 出错返回空
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <returns></returns>
public string RunSqlReturn(string strSql)
{
#region
string strReturn = "";
Open();
try
{
cmd = new SqlCommand(strSql, cn);
strReturn = cmd.ExecuteScalar().ToString();
}
catch
{
strReturn = null;
}
Close();
return strReturn;
#endregion
}
/// <summary>
/// 执行SQL语句,并返回name字段的结果集合 出错返回空
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <param name="name">字段名</param>
/// <returns></returns>
public List<string> RunSqlReturn(string strSql,string name )
{
#region
List<string> strReturn = new List<string>();
Open();
try
{
cmd = new SqlCommand(strSql, cn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
strReturn.Add(reader[name] as string);
}
}
catch
{
strReturn = null;
}
Close();
return strReturn;
#endregion
}
/// <summary>
/// 未知
/// </summary>
/// <param name="dataTable"></param>
/// <param name="size"></param>
public void UpdateChange(DataTable dataTable, Int32 size)
{
Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.UpdateCommand = new SqlCommand("update agenda set sortId=@sortId where Id=@Id", cn);
sda.UpdateCommand.Parameters.Add("@sortId", SqlDbType.Int, 4, "sortId");
sda.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int, 4, "ID");
sda.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
sda.UpdateBatchSize = size;
sda.Update(dataTable);
}
/// <summary>
/// 通过command对象执行DML语句,主要完成删除,添加和修改
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public bool execmdnonqy(string strSql)
{
Open();
try
{
cmd = new SqlCommand(strSql, cn);
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally {
Close();
}
}
}
}