using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Text; namespace Terra.YaoGan.Common { public sealed class sqlserverHelper : IDatabaseInterface { private static readonly object lk = new object(); private SqlConnection con = null; private static sqlserverHelper db = null; private string cstr = ConfigurationManager.ConnectionStrings["connString"].ConnectionString; #region 数据库连接 public static sqlserverHelper getInstance() { if (db == null) { lock (lk) { if (db == null) { db = new sqlserverHelper(); } } } return db; } public void connect() { try { con = new SqlConnection(cstr); if (con.State == ConnectionState.Closed) { con.Open(); } } catch (Exception e) { } } public void close() { try { if (con != null) { con.Dispose(); con.Close(); } } catch(Exception e) { } } public int getRow(string sql) { int result = -1; try { connect(); result = new SqlCommand(sql, con).ExecuteNonQuery(); close(); } catch(Exception e) { close(); } return result; } public DataTable QuerySql(string sql) { DataTable dt = new DataTable(); try { connect(); SqlCommand cmd = new SqlCommand(sql, con); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); close(); } catch(Exception e) { close(); } return dt; } #endregion } }