using Logs;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using cuidian.Common;
namespace cuidian.Sql
{
public class DbUtils
{
///
/// 返回第一行第一列
///
///
///
///
public static object ExecuteScalar(string sql, SqlConnection con)
{
SqlCommand cmd = null;
object rtn = null;
try
{
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
cmd = new SqlCommand(__ProcessSql(sql), con);
rtn = cmd.ExecuteScalar();
}
catch (Exception e)
{
LogHelper.Debug(e.Message, e);
LogHelper.Debug("错误sql语句:"+ sql);
throw e;
}
finally
{
if (con.State == ConnectionState.Open) con.Close();
}
return rtn;
}
///
/// 返回语句执行受影响的
///
///
///
///
public static int ExecuteNonQuery(string sql, SqlConnection con)
{
int rtn = 0;
SqlCommand cmd;
try
{
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand(__ProcessSql(sql), con);
rtn = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
LogHelper.Debug(ex.Message);
LogHelper.Debug(sql);
throw ex;
}
finally
{
if (con.State == ConnectionState.Open) con.Close();
}
return rtn;
}
///
/// 填充datatable
///
///
///
///
public static DataTable Fill(string sql, SqlConnection con)
{
SqlCommand cmd = new SqlCommand(__ProcessSql(sql), con);
return Fill(cmd, con);
}
public static DataTable Fill(SqlCommand cmd, SqlConnection con)
{
DataTable dt = new DataTable();
try
{
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
int i = da.Fill(dt);
}
catch (Exception e)
{
Logs.LogHelper.Debug(cmd.CommandText);
Logs.LogHelper.Debug(e.Message, e);
throw e;
}
finally
{
con.Close();
}
return dt;
}
public static DataSet Fill(Dictionary sqls, SqlConnection con)
{
DataSet ds = new DataSet();
SqlCommand cmd;
try
{
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
foreach (var item in sqls)
{
cmd = new SqlCommand(item.Value, con);
DataTable dt = ds.Tables[item.Key];
if (dt == null)
{
dt = Fill(__ProcessSql( item.Value), con);
dt.TableName = item.Key;
ds.Tables.Add(dt);
}
}
}
catch (Exception e)
{
Logs.LogHelper.Debug(e.Message, e);
throw e;
}
finally
{
con.Close();
}
return ds;
}
///
/// 处理特殊字符
///
///
///
private static string __ProcessSql(string sql)
{
//string rtn = sql.Replace("'", "''");
return sql;
}
public static bool IsHaveObject(string name,EnumDataBaseOjbect type, string accId = "")
{
if(accId == "") accId = AppSeting.Instance.GetValue("default");
switch (type)
{
case EnumDataBaseOjbect.Procedure:
return __IsHaveProcedure(name,accId);
break;
case EnumDataBaseOjbect.Table:
break;
case EnumDataBaseOjbect.View:
break;
default:
break;
}
return false;
}
private static bool __IsHaveProcedure(string name, string accid)
{
string sql = "select count(1) from sysobjects where id = object_id('{0}') and xtype='p'";
string rtn = ExecuteScalar(string.Format(sql,name), ConnectionUtils.Instance.GetConnection(accid)).ToString();
return rtn == "1";
}
}
}