#region 执行SQL不带返回记录
public static bool ExecuteNonQuery(string commandText, OleDbConnection connection, CommandType commandType, int nSrcID, OleDbTransaction trans)
{
TraceSql(commandText); bool bNeedClose = false;
try
{
if (connection == null)
{
// Create a connection
if (nSrcID < 0) nSrcID = defaltConnect;
connection = new OleDbConnection(GetConnectionString(nSrcID));
} if (connection.State != ConnectionState.Open)
{
connection.Open();
bNeedClose = true;
} // Create a command and prepare it for execution
OleDbCommand cmd = new OleDbCommand(commandText, connection, trans);
// Finally, execute the command
int retval = cmd.ExecuteNonQuery(); // Detach the SqlParameters from the command object, so they can be used again
if (bNeedClose)
connection.Close(); return true;
}
catch
{
if (bNeedClose)
connection.Close(); return false;
}
} public static bool ExecuteNonQuery(string commandText)
{
return ExecuteNonQuery(commandText, null, CommandType.Text, defaltConnect, null);
} public static bool ExecuteNonQuery(string commandText, int nSrcID)
{
return ExecuteNonQuery(commandText, null, CommandType.Text, nSrcID, null);
} public static bool ExecuteNonQuery(string commandText, OleDbConnection connection)
{
return ExecuteNonQuery(commandText, connection, CommandType.Text, defaltConnect, null);
} public static bool ExecuteNonQuery(string commandText, OleDbTransaction trans)
{
return ExecuteNonQuery(commandText, trans.Connection, CommandType.Text, defaltConnect, trans);
} public static bool ExecuteNonQuery(string commandText, OleDbConnection connection, OleDbTransaction trans)
{
return ExecuteNonQuery(commandText, connection, CommandType.Text, defaltConnect, trans);
} #endregion #region 执行SQL带有返回结果(DataSet) public static DataSet ExecuteDataset(string commandText, OleDbConnection connection, CommandType commandType, int nSrcID, OleDbTransaction trans)
{
TraceSql(commandText); bool bNeedClose = false;
try
{
if (connection == null)
{
if (nSrcID < 0) nSrcID = defaltConnect; connection = new OleDbConnection(GetConnectionString(nSrcID));
} if (connection.State != ConnectionState.Open)
{
connection.Open();
bNeedClose = true;
}
// Create a command and prepare it for execution
OleDbCommand cmd = new OleDbCommand(commandText, connection, trans); // Create the DataAdapter & DataSet
using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{
DataSet ds = new DataSet(); // Fill the DataSet using default values for DataTable names, etc
da.Fill(ds); //close connection for later using.
if (bNeedClose)
connection.Close(); // Return the dataset
return ds;
}
}
catch (Exception ee)
{
if (bNeedClose)
connection.Close();
throw ee;
//return null;
}
} public static DataSet ExecuteDataset(string commandText)
{
return ExecuteDataset(commandText, null, CommandType.Text, defaltConnect, null);
} public static DataSet ExecuteDataset(string commandText, OleDbConnection connection)
{
return ExecuteDataset(commandText, connection, CommandType.Text, defaltConnect, null);
} public static DataSet ExecuteDataset(string commandText, int nSrcID)
{
return ExecuteDataset(commandText, null, CommandType.Text, nSrcID, null);
} public static DataSet ExecuteDataset(string commandText, OleDbTransaction trans)
{
return ExecuteDataset(commandText, trans.Connection, CommandType.Text, defaltConnect, trans);
} #endregion #region 执行SQLReader
public static OleDbDataReader ExecuteReader(string commandText, OleDbConnection connection, CommandType commandType, int nSrcID, OleDbTransaction trans)
{
TraceSql(commandText);
bool bNeedClose = false;
try
{
//create connection
if (connection == null)
{
if (nSrcID < 0) nSrcID = defaltConnect;
connection = new OleDbConnection(GetConnectionString(nSrcID));
} if (connection.State != ConnectionState.Open)
{
connection.Open();
bNeedClose = true;
} // Create a command and prepare it for execution
OleDbCommand cmd = new OleDbCommand(commandText, connection);
cmd.Transaction = trans;
//return reader
OleDbDataReader reader = null; if (bNeedClose)
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
else
reader = cmd.ExecuteReader(); return reader;
}
catch (Exception ee)
{
if (bNeedClose)
connection.Close(); throw ee;
// return null;
}
} public static OleDbDataReader ExecuteReader(string commandText)
{
return ExecuteReader(commandText, null, CommandType.Text, defaltConnect, null);
} public static OleDbDataReader ExecuteReader(string commandText, OleDbConnection connection)
{
return ExecuteReader(commandText, connection, CommandType.Text, defaltConnect, null);
} public static OleDbDataReader ExecuteReader(string commandText, int nSrcID)
{
return ExecuteReader(commandText, null, CommandType.Text, nSrcID, null);
} public static OleDbDataReader ExecuteReader(string commandText, OleDbTransaction trans)
{
return ExecuteReader(commandText, trans == null ? null : trans.Connection, CommandType.Text, defaltConnect, trans);
}
#endregion
解决方案 »
- 使用 Developer Express 中的Xtrabars设计菜单,如何增加分隔线,将菜单分组
- C#多线程中 出现点问题....
- 字符串处理输入问题
- datalist控件的每一个itemtemplate的backcolor可以分别设置吗?
- web gridview控件在编辑状态下怎么获得哪个控件自事宾类似textbox的值?
- VS2005如何在VB开发环境下添加C#类??
- 保存页面状态问题
- 请问这里的朋友,VS2012编译的程序如何在XP上能运行
- 请问这个2个正则表达式是什么意思 谢谢
- vs.net中的正则表达式
- [求助]C#的问题!DataGrid控件,不能编辑DataTable元素怎么办?
- 想我这种GridView如何取这行的值,请赐教啊!
public static bool ExeStoredProcedure(string procedureName, OleDbConnection connection, int nSrcID, ArrayList arrInParaType, ArrayList arrInParaValue, ArrayList arrOutParaType, ArrayList arrOutParaValue, OleDbTransaction trans)
{
bool okExecute = false;
bool bNeedClose = false;
try
{
//create connection
if (connection == null)
{
if (nSrcID < 0) nSrcID = defaltConnect;
connection = new OleDbConnection(GetConnectionString(nSrcID));
} if (connection.State != ConnectionState.Open)
{
connection.Open();
bNeedClose = true;
} // Create a command and prepare it for execution
OleDbCommand cmd = new OleDbCommand(procedureName, connection);
if (trans != null)
cmd.Transaction = trans; cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear(); if (arrInParaType != null)
{
for (int i = 0; i < arrInParaType.Count; i++)
{
OleDbParameter p = new OleDbParameter();
p.OleDbType = (OleDbType)arrInParaType[i];
p.Value = arrInParaValue[i];
p.Direction = ParameterDirection.Input;
cmd.Parameters.Add(p);
}
} if (arrOutParaType != null)
{
for (int i = 0; i < arrOutParaType.Count; i++)
{
OleDbParameter p = new OleDbParameter();
p.OleDbType = (OleDbType)arrOutParaType[i];
p.Direction = ParameterDirection.Output;
if (p.OleDbType == OleDbType.VarChar)
p.Size = 1000;
else
p.Size = 30; cmd.Parameters.Add(p);
}
} cmd.ExecuteNonQuery(); if (arrInParaType != null)
{
arrOutParaValue.Clear();
for (int i = arrInParaType.Count; i < arrOutParaType.Count + arrInParaType.Count; i++)
{
OleDbType outType = (OleDbType)arrOutParaType[i - arrInParaType.Count];
object outValue = cmd.Parameters[i].Value;
if (outType == OleDbType.VarChar)
{
string outString = outValue.ToString();
if (outString != null)
arrOutParaValue.Add(outString.Trim('\0'));
else
arrOutParaValue.Add(outString);
}
else
{
arrOutParaValue.Add(outValue);
}
}
} if (bNeedClose)
connection.Close(); okExecute = true;
}
catch
{
if (bNeedClose)
{
connection.Close();
}
okExecute = false;
} TraceSql(procedureName, arrInParaType, arrInParaValue, arrOutParaType, arrOutParaValue);
return okExecute;
} public static bool ExeStoredProcedure(string procedurename, ArrayList arrInParaType, ArrayList arrInParaValue, ArrayList arrOutParaType, ArrayList arrOutParaValue)
{
return ExeStoredProcedure(procedurename, null, defaltConnect, arrInParaType, arrInParaValue, arrOutParaType, arrOutParaValue, null);
} public static bool ExeStoredProcedure(string procedurename, ArrayList arrInParaType, ArrayList arrInParaValue, ArrayList arrOutParaType, ArrayList arrOutParaValue, OleDbTransaction trans)
{
return ExeStoredProcedure(procedurename, null, defaltConnect, arrInParaType, arrInParaValue, arrOutParaType, arrOutParaValue, trans);
} public static bool ExeStoredProcedure(string procedurename, OleDbConnection connection, ArrayList arrInParaType, ArrayList arrInParaValue, ArrayList arrOutParaType, ArrayList arrOutParaValue)
{
return ExeStoredProcedure(procedurename, connection, defaltConnect, arrInParaType, arrInParaValue, arrOutParaType, arrOutParaValue, null);
} public static bool ExeStoredProcedure(string procedurename, int nSrcID, ArrayList arrInParaType, ArrayList arrInParaValue, ArrayList arrOutParaType, ArrayList arrOutParaValue)
{
return ExeStoredProcedure(procedurename, null, nSrcID, arrInParaType, arrInParaValue, arrOutParaType, arrOutParaValue, null);
}
各位大哥 这是别人封状好的操作数据库的类库 我要在display 的 页面上的饿label里面显示数据库中的一条记录或着整个表的内容 怎么写 详细代码!!!!!!!!!!!!!!!
你可以在datatable中将数据读出来放到lable上就可以了