C# 第二个问题(与原始代码有关)是,在退出Connect()方法后,OleDbConnection对象没有句柄,因此它由GC处理,因此无论如何它都是关闭的。@Ulfric:那么,你到底试过了吗?哇-首字母缩略词发烧!数据访问对象。这些是一个将数据访问代码放入其
C# 第二个问题(与原始代码有关)是,在退出Connect()方法后,OleDbConnection对象没有句柄,因此它由GC处理,因此无论如何它都是关闭的。@Ulfric:那么,你到底试过了吗?哇-首字母缩略词发烧!数据访问对象。这些是一个将数据访问代码放入其,c#,oracle,oracle11g,database-connection,C#,Oracle,Oracle11g,Database Connection,第二个问题(与原始代码有关)是,在退出Connect()方法后,OleDbConnection对象没有句柄,因此它由GC处理,因此无论如何它都是关闭的。@Ulfric:那么,你到底试过了吗?哇-首字母缩略词发烧!数据访问对象。这些是一个将数据访问代码放入其中的类,以使其与应用程序的业务逻辑分离。我第一次使用它是在Java时代。看。 private bool Connect(string username, string password) { string CONNSTR
第二个问题(与原始代码有关)是,在退出Connect()方法后,OleDbConnection对象没有句柄,因此它由GC处理,因此无论如何它都是关闭的。@Ulfric:那么,你到底试过了吗?哇-首字母缩略词发烧!数据访问对象。这些是一个将数据访问代码放入其中的类,以使其与应用程序的业务逻辑分离。我第一次使用它是在Java时代。看。
private bool Connect(string username, string password)
{
string CONNSTRING = "Provider = MSDAORA; Data Source = ISDQA; User ID = {0}; Password = {1};";
OleDbConnection conn = new OleDbConnection();
string strCon = string.Format(CONNSTRING, username, password);
conn.ConnectionString = strCon;
bool isConnected = false;
try
{
conn.Open();
if (conn.State.ToString() == "Open")
isConnected = true;
}//try
catch (Exception ex)
{
lblErr.Text = "Connection error";
}//catch
finally
{
conn.Close();
}//finally
return isConnected;
}
private bool ValidateUserCode(string usercode)
{
UserAccountDefine def = new UserAccountDefine();
UserAccountService srvc = new UserAccountService();
UserAccountObj obj = new UserAccountObj();
bool returnVal = false;
bool isValid = Connect(def.DB_DUMMY_USERCODE, def.DB_DUMMY_PASSWORD);
if (isValid)
{
obj.SQLQuery = string.Format(def.SQL_LOGIN, usercode.ToLower(), DateTime.Now.ToString("MM/dd/yyy"));
DataTable dt = srvc.Execute(obj, CRUD.READALL);
if (dt.Rows.Count == 1)
{
returnVal = true;
}
}
return returnVal;
}
if (con != null)
{
con.Close();
con.Dispose();
}
try
{
con = new OleDbConnection(connectionString);
}
catch (Exception ex)
{
MessageBox.Show("oledbConnection = " + ex.Message);
}
try
{
con.Open();
}
catch (Exception ex)
{
MessageBox.Show("connection open = " + ex.Message + "\n");
}
private OleDbConnection Connect(string username, string password)
{
string CONNSTRING = "Provider = MSDAORA; Data Source = ISDQA; User ID = {0}; Password = {1};";
OleDbConnection conn = new OleDbConnection();
string strCon = string.Format(CONNSTRING, username, password);
conn.ConnectionString = strCon;
try
{
conn.Open();
if (conn.State.ToString() == "Open")
return conn;
}//try
catch (Exception ex)
{
lblErr.Text = "Connection error";
}//catch
finally
{
//you don't want to close it here
//conn.Close();
}//finally
return null;
}
public DataSet FetchDataSet(string sql, IDictionary paramHash) {
var cnn = AcquireConnection();
var rtnDS = new DataSet();
try
{
var cmd = cnn.CreateCommand();
cmd.CommandText = sql;
SetParameters(cmd, paramHash);
IDbDataAdapter ida = new DataAdapter { SelectCommand = cmd };
LogSql(sql, paramHash, "FetchDataSet");
ida.Fill(rtnDS);
}
catch (Exception ex)
{
DebugWriteLn("Failed to get a value from the db.", ex);
throw;
}
finally
{
ReleaseConnection(cnn);
}
return rtnDS;
}
public MyItemType FindSomeValue(long Id)
{
const string sql = @"SELECT something from somewhere where id=:id";
var myParams = new Dictionary<string, long> { { "id", Id } };
var ds = FetchDataSet(sql, myParams);
return (from DataRow row in ds.Tables[0].Rows
select new Item
{
Id = Convert.ToInt64(row["ID"], CultureInfo.InvariantCulture),
Name = row["NAME"].ToString()
}).FirstOrDefault();
}