123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- 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
- {
- /// <summary>
- /// 返回第一行第一列
- /// </summary>
- /// <param name="sql"></param>
- /// <param name="constr"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 返回语句执行受影响的
- /// </summary>
- /// <param name="sql"></param>
- /// <param name="constr"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 填充datatable
- /// </summary>
- /// <param name="sql"></param>
- /// <param name="con"></param>
- /// <returns></returns>
- 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<string, string> 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;
- }
- /// <summary>
- /// 处理特殊字符
- /// </summary>
- /// <param name="sql"></param>
- /// <returns></returns>
- 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";
- }
-
- }
- }
|