using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Xml;
using System.Collections;
namespace Com.Richinfo.Framework.Utils
{
class SysBaseUtils
{
private static SqlConnection con;
private static SqlCommand cmd;
private static DataSet ds;
private static SqlDataAdapter da;
#region 获取数据库连接对象
/// <summary>
/// 获取数据库连接对象
/// </summary>
/// <returns> SqlConnection </returns>
public static SqlConnection GetDBConnection()
{
string ConnStr = GetDBConnectionString();
try
{
con = new SqlConnection(ConnStr);
return con;
}
catch //(Exception ex)
{
//System.Console.Out.Write(ex.Message);
ShowError( "连接数据库失败。 ");
return null;
}
}
#endregion
#region 获取连接字符串
/// <summary>
/// 获取连接字符串
/// </summary>
/// <returns> string </returns>
public static string GetDBConnectionString()
{
string text = Application.StartupPath + "//config ";
try
{
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
XmlTextReader xtr = new XmlTextReader(text + "//data.xml ");
while (xtr.Read())
{
if (xtr.Name == "STRING ")
{
xtr.Read();
return xtr.Value;
}
}
xtr.Close();
return null;
}
catch //(Exception ex)
{
//System.Console.Out.Write(ex.Message);
ShowError( "获取不到数据库连接。 ");
return null;
}
}
#endregion
#region 查询操作,并且返回记录集
/// <summary>
/// 查询操作,并且返回记录集
/// </summary>
/// <param name= "StrSQL "> 查询SQL语句 </param>
/// <param name= "obj "> </param>
/// <returns> DataSet </returns>
public static DataSet ExecuteQuery(string StrSQL, Hashtable obj)
{
GetDBConnection();
try
{
con.Open();
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = StrSQL;
cmd.Connection = con;
if (obj != null)
{
IDictionaryEnumerator numerator = obj.GetEnumerator();
while (numerator.MoveNext())
{
cmd.Parameters.AddWithValue(numerator.Entry.Key.ToString(), numerator.Entry.Value);
}
}
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
for (int i = 0; i < cmd.Parameters.Count; i++)
{
cmd.Parameters.Remove(cmd.Parameters[i]);
}
cmd.Dispose();
con.Close();
con.Dispose();
return ds;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
#endregion
#region 查询操作,并且返回记录集
/// <summary>
/// 查询操作,并且返回记录集
/// </summary>
/// <param name= "StrSQL "> 查询SQL语句 </param>
/// <param name= "obj "> </param>
/// <returns> DataSet </returns>
public static DataSet ExecuteQuery(string StrSQL)
{
GetDBConnection();
try
{
con.Open();
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = StrSQL;
cmd.Connection = con;
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
for (int i = 0; i < cmd.Parameters.Count; i++)
{
cmd.Parameters.Remove(cmd.Parameters[i]);
}
cmd.Dispose();
con.Close();
con.Dispose();
return ds;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
#endregion
#region 获取DataReader
/// <summary>
/// 获取DataReader
/// </summary>
/// <param name= "strSQL "> SQL语句 </param>
/// <param name= "obj "> </param>
/// <returns> SqlDataReader </returns>
public static SqlDataReader GetReaderCloseDb(string strSQL, Hashtable obj)
{
GetDBConnection();
try
{
con.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSQL;
cmd.Connection = con;
if (obj != null)
{
IDictionaryEnumerator numerator = obj.GetEnumerator();
while (numerator.MoveNext())
{
cmd.Parameters.AddWithValue(numerator.Entry.Key.ToString(), numerator.Entry.Value);
}
}
SqlDataReader dr = cmd.ExecuteReader();
for (int i = 0; i < cmd.Parameters.Count; i++)
{
cmd.Parameters.Remove(cmd.Parameters[i]);
}
return dr;
}
catch
{
MessageBox.Show( "数据库连接错误! ");
return null;
}
}
#endregion
#region 数据增、删、改操作,成功返回1,失败返回0
/// <summary>
/// 数据增、删、改操作,成功返回1,失败返回0
/// </summary>
/// <param name= "StrSQL "> 增、删、改SQL语句 </param>
/// <param name= "obj "> </param>
/// <returns> int </returns>
public static int ExecuteUpdate(string StrSQL, Hashtable obj)
{
GetDBConnection();
try
{
con.Open();
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = StrSQL;
cmd.Connection = con;
if (obj != null)
{
IDictionaryEnumerator numerator = obj.GetEnumerator();
while (numerator.MoveNext())
{
cmd.Parameters.AddWithValue(numerator.Entry.Key.ToString(), numerator.Entry.Value);
}
}
return cmd.ExecuteNonQuery();
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
#endregion
#region 数据增、删、改操作,成功返回1,失败返回0
/// <summary>
/// 数据增、删、改操作,成功返回1,失败返回0
/// </summary>
/// <param name= "StrSQL "> 增、删、改SQL语句 </param>
/// <param name= "obj "> </param>
/// <returns> int </returns>
public static int ExecuteUpdate(string StrSQL)
{
GetDBConnection();
try
{
con.Open();
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = StrSQL;
cmd.Connection = con;
return cmd.ExecuteNonQuery();
}
finally //(Exception ex)
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
#endregion
sql数据库操作(c#+net )
最新推荐文章于 2024-11-13 22:17:16 发布