public object ExecuteScalar(string sqlStr) { lock (_obj) { using (TAdoDbxConnection conn = new TAdoDbxConnection(_connectionString)) { TAdoDbxCommand cmd = new TAdoDbxCommand(); cmd.Connection = conn; cmd.CommandText = sqlStr; try { cmd.Connection.Open(); return(cmd.ExecuteScalar()); } catch (Exception ex) { _logger.Error(string.Format("执行SQL错误:{0}.", ex)); return(null); } finally { cmd.Connection.Close(); cmd.Connection.Dispose(); cmd.Dispose(); } } } }
public int ExecuteNonQuery(string sqlStr) { lock (_obj) { using (TAdoDbxConnection conn = new TAdoDbxConnection(_connectionString)) { int NonQuery = 0; TAdoDbxCommand cmd = new TAdoDbxCommand(); cmd.Connection = conn; cmd.CommandText = sqlStr; try { cmd.Connection.Open(); NonQuery = cmd.ExecuteNonQuery(); } catch (Exception ex) { if (ex.Message != "message length error (encountered 0, expected 8)" && ex.Message != "message length error (encountered 0, expected 12)") { _logger.Error(string.Format("执行SQL错误:{0}.", ex)); } return(NonQuery); } finally { cmd.Connection.Close(); cmd.Connection.Dispose(); cmd.Dispose(); } return(NonQuery); } } }
public DataTable GetData(string sqlStr) { lock (_obj) { using (TAdoDbxConnection conn = new TAdoDbxConnection(_connectionString)) { DataSet ds = new DataSet(); TAdoDbxCommand cmd = new TAdoDbxCommand(); cmd.Connection = conn; cmd.CommandText = sqlStr; try { cmd.Connection.Open(); DbDataReader myreader = cmd.ExecuteReader(); DataTable dt = new DataTable(); if (myreader.HasRows) { if (!Convert.IsDBNull(myreader)) { dt.Load(myreader); ds.Tables.Add(dt); ds.Load(myreader, LoadOption.PreserveChanges, ds.Tables[0]); } else { ds.Tables.Add(dt); } } else { ds.Tables.Add(dt); } myreader.Close(); myreader.Dispose(); } catch (Exception ex) { _logger.Error(string.Format("执行SQL错误:{0}.", ex)); } finally { cmd.Connection.Close(); cmd.Connection.Dispose(); cmd.Dispose(); } if (ds.Tables.Count > 0) { return(ds.Tables[0]); } else { return(new DataTable()); } } } }