CEOSQL Server 2000DBAVisual Studio .NET 2003.NET 1.1Windows 2000架构师Windows XP中级开发Visual StudioSQL ServerSQLWindows.NETC#
用 C# 编写的 SQL 数据库连接 DLL






1.90/5 (7投票s)
2005年9月8日
1分钟阅读

111027

2341
使用事件日志连接和关闭 SQL 数据库,并带有内部错误报告。
引言
这是一个用 C# 编写的易于使用的 DLL,用于打开和关闭数据库。该 DLL 具有内部事件日志记录功能,以便在 DLL 遇到错误时进行记录。事件日志将被记录到 source db error。以下是私有和公共委托和成员的列表。
公共委托
SqlConnection Connection - 返回实际的数据库连接。
String Database - 设置或返回数据库名称的字符串值。
String Server - 设置或返回服务器名称的字符串值。
String Sapwd - 设置或返回 SQL 身份验证密码的字符串值。
公共成员
IsOpen() - 确定连接的状态。如果打开则返回 true,如果关闭则返回 false。
Open() - 打开数据库。如果成功打开则返回 true。如果发生错误则返回 false。然后将错误记录到事件日志。
Close() - 关闭数据库。如果成功关闭则返回 true。如果发生错误则返回 false。将错误记录到事件日志。
setConnection() - 将连接字符串设置为 m_conn 的 SqlConnection。
(要使用此成员,您必须首先通过第一个构造函数构造数据库,或者您必须提交如上所述的单个委托。)
私有成员
errTrack() - 设置一个新的错误日志条目,并使日志成为事件查看器的错误。
MessageError() - 如果发生错误,它将显示一个消息框,解释发生了错误,并将它们转发到事件查看器。
这是一种直接的数据库连接方法。
要使用此方法,您可以使用以下两个构造函数之一。请参阅下面的示例执行。
string dbName = "testdb";
string serverName = "(local)";
string sapwd = "sapwd";
/// Using the standard constructor and inputing all information
/// From the constructor. This will automatically instantiate the setConnection Member
private dbx32sql.Connect conn = new dbx32sql.Connect(dbName, serverName, sapwd);
/// Using the non standard constructor with no inputs. Must initiate setConnection;
private dbx32sql.Connect conn = new dbx32sql.Connect();
conn.Database = dbName;
conn.Server = serverName;
conn.Sapwd = sapwd;
conn.setConnection;
try
{
if (conn.Open)
{
string sqlGet = "SELECT COUNT (ID) FROM authors";
SqlCommand cmdGet = new SqlCommand(sqlGet, conn.Connection);
int Count = (int)cmdGet.ExecuteScalar();
CmdGet.Dispose();
}
else
{
// Input your error handling here;
}
}
catch(Exception ex)
{
//insert other error handling.
}
finally
{
conn.Close();
}