using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace YF.MsSqlHelper
{
public static class YFMsSqlHelper
{
//连接字符串
public static readonly string connectionString = ConfigurationManager.ConnectionStrings["xiaobai"].ConnectionString;
public static int GetMaxID(string FieldName,string TableName)
{
string strsql = "select max(" + FieldName +") +1 from * + TableName;
object obj = GetString(strsql);
if(obj == null)
{
return 1;
}
else
{
return int.paras(obj.ToString());
}
}
public static bool Exists(string strSql)
{
object obj = GetString(strSql);
int cmdresult;
if(Object.Equals(obj,null)) || (Object.Equals(obj,System.DBNull.Value)))
{
cmdresult =0;
}
else
{
cmdresult = int.Parse(obj.Tostrign());
}
if(cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
//执行SQL语句,返回影响的记录数
public static int ExecuteSql(string SQLString)
{
using(SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch(SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
//设置SQL语句,设置命令的执行等待时间
public static int ExecuteSqlByTime(string SQLString ,int Times)
{
using (SqlConnection cmd = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString,connection))
{
try
{
connection.Open();
cmd.CommandTimeout = Times;
int rows = cmd.ExecuteNonQuery();
}
catch(SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
//执行多条SQL语句,实现数据库事务
public static void ExecuteSqlTran(ArrayList SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for(int n=0; n<SQLStringList.Count;n++)
{
string strsql = SQLStringList[n].ToString();
if(strsql.Trim().Length >1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
//执行一个带存储参数过程的SQL语句
public static int ExecuteSql(string SQLString,string content)
{
using (SqlConnection connection = new SqlConnection(connection))
{
SqlCommand cmd = new SqlCommand(SQLString,connection);
SqlParameter myParameter = new SqlParameter("@content",SqlDbType.NText);
myParameter.Value = content;
cmd.Parameters.Add(myparameter);
try
{
connection.Open();
int rows =cmd.ExecuteNonQuery();
return rows;
}
catch(SqlException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
//执行带一个存储过程参数的SQL语句
public static object ExecuteSqlGet(string SQLString , string content)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(SQLString , connection);
SqlParameter myParameter = new SqlParameter("@content",SqlDbType.Ntext);
myParameter.Value = content;
cmd.Parameter.Add(myParameter);
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if(Object.Equals(obj,null)) || (Object.Equals(obj,System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (SqlException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
//向数据库中插入图像格式的字段(和上面情况类似的一种实例)
public static int ExecuteSqlInsertImg(string strSQL,byte [] fs)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(strSQL,connection);
SqlParameter myParameter = new SqlParameter("@fs",SqlDbType.Image);
myParameter.Value = fs;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch(SqlException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
MsSqlHelper类库
最新推荐文章于 2024-06-10 11:42:11 发布