我常用的C# 操作MSSQL数据库类(winform)

发布时间:2009年06月11日      浏览次数:512 次
using System;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace MyData
{
/// <summary>
/// 通用数据库类MSSQL
/// </summary>
public class Class_mssql_conn
{
//public static string ConnStr = @"server=服务器;uid=帐号;pwd=密码;database=数据库;";
public static string ConnStr = MyData.Properties.Settings.Default.my_soft_sqlConn + "Password=密码;";
//打开数据库链接
public static SqlConnection Open_Conn(string ConnStr)
{
SqlConnection Conn = new SqlConnection(ConnStr);
Conn.Open();
return Conn;
}
//关闭数据库链接
public static void Close_Conn(SqlConnection Conn)
{
if (Conn != null)
{
Conn.Close();
Conn.Dispose();
}
GC.Collect();
}
//运行Sql语句
public static int Run_SQL(string SQL, string ConnStr)
{
SqlConnection Conn = Open_Conn(ConnStr);
SqlCommand Cmd = Create_Cmd(SQL, Conn);
try
{
int result_count = Cmd.ExecuteNonQuery();
Close_Conn(Conn);
return result_count;
}
catch
{
Close_Conn(Conn);
return 0;
}
}
// 生成Command对象
public static SqlCommand Create_Cmd(string SQL, SqlConnection Conn)
{
SqlCommand Cmd = new SqlCommand(SQL, Conn);
return Cmd;
}
// 运行Sql语句返回 DataTable
public static DataTable Get_DataTable(string SQL, string ConnStr, string Table_name)
{
SqlDataAdapter Da = Get_Adapter(SQL, ConnStr);
DataTable dt = new DataTable(Table_name);
Da.Fill(dt);
return dt;
}
// 运行Sql语句返回 SqlDataReader对象
public static SqlDataReader Get_Reader(string SQL, string ConnStr)
{
SqlConnection Conn = Open_Conn(ConnStr);
SqlCommand Cmd = Create_Cmd(SQL, Conn);
SqlDataReader Dr;
try
{
Dr = Cmd.ExecuteReader(CommandBehavior.Default);
}
catch
{
throw new Exception(SQL);
}
Close_Conn(Conn);
return Dr;
}
// 运行Sql语句返回 SqlDataAdapter对象
public static SqlDataAdapter Get_Adapter(string SQL, string ConnStr)
{
SqlConnection Conn = Open_Conn(ConnStr);
SqlDataAdapter Da = new SqlDataAdapter(SQL, Conn);
return Da;
}
// 运行Sql语句,返回DataSet对象
public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds)
{
SqlDataAdapter Da = Get_Adapter(SQL, ConnStr);
try
{
Da.Fill(Ds);
}
catch (Exception Err)
{
throw Err;
}
return Ds;
}
// 运行Sql语句,返回DataSet对象
public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds, string tablename)
{
SqlDataAdapter Da = Get_Adapter(SQL, ConnStr);
try
{
Da.Fill(Ds, tablename);
}
catch (Exception Ex)
{
throw Ex;
}
return Ds;
}
// 运行Sql语句,返回DataSet对象,将数据进行了分页
public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds, int StartIndex, int PageSize, string tablename)
{
SqlConnection Conn = Open_Conn(ConnStr);
SqlDataAdapter Da = Get_Adapter(SQL, ConnStr);
try
{
Da.Fill(Ds, StartIndex, PageSize, tablename);
}
catch (Exception Ex)
{
throw Ex;
}
Close_Conn(Conn);
return Ds;
}
// 返回Sql语句执行结果的第一行第一列
public static string Get_Row1_Col1_Value(string SQL, string ConnStr)
{
SqlConnection Conn = Open_Conn(ConnStr);
string result;
SqlDataReader Dr;
try
{
Dr = Create_Cmd(SQL, Conn).ExecuteReader();
if (Dr.Read())
{
result = Dr[0].ToString();
Dr.Close();
}
else
{
result = "";
Dr.Close();
}
}
catch
{
throw new Exception(SQL);
}
Close_Conn(Conn);
return result;
}
}
}
------------------------------------------------------------------
app.config 里的数据库链接字符串
------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="MySoft.Properties.Settings.my_soft_Conn" connectionString="Data Source=localhost;Initial Catalog=my_soft;Persist Security Info=True;User ID=sa;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
文章来源:桂林唯创网络
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!