📄 connection.cs
字号:
using System;
namespace _36Hang.Data.SqlClient
{
/// <summary>
/// Connection 的摘要说明。
/// </summary>
public class Connection
{
private string _Server;
private string _Database;
private string _Username;
private string _Password;
private System.Data.SqlClient.SqlConnection objSqlConnection;
public Connection(string Server,string Database,string Username,string Password)
{
_Server = Server;
_Database = Database;
_Username = Username;
_Password = Password;
}
/// <summary>
/// 打开数据库连接
/// </summary>
private void Open()
{
try
{
string strConnection = "server=" + _Server + ";";
strConnection += "database=" + _Database + ";";
strConnection += "uid=" + _Username + ";";
strConnection += "pwd=" + _Password;
objSqlConnection = new System.Data.SqlClient.SqlConnection(strConnection);
objSqlConnection.Open();
}
catch(Exception e)
{
throw(e);
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
private void Close()
{
try
{
objSqlConnection.Close();
}
catch(Exception e)
{
throw(e);
}
}
/// <summary>
/// 执行数据库查询
/// </summary>
/// <param name="Sql">数据库查询语句</param>
public void Execute(string Sql)
{
try
{
Open();
try
{
System.Data.SqlClient.SqlCommand objSqlCommand = new System.Data.SqlClient.SqlCommand(Sql,objSqlConnection);
objSqlCommand.ExecuteNonQuery();
Close();
}
catch(Exception e)
{
Close();
throw(e);
}
}
catch(Exception e)
{
throw(e);
}
}
/// <summary>
/// 获得数据库查询结果
/// 用于获得全部查询结果
/// </summary>
/// <param name="Sql">数据库查询语句</param>
/// <returns>数据库查询结果</returns>
public string Records(string Sql)
{
try
{
Open();
try
{
string strRecords = "<?xml version=\"1.0\" encoding=\"gb2312\" ?><Root>";
System.Data.SqlClient.SqlCommand objSqlCommand = new System.Data.SqlClient.SqlCommand(Sql,objSqlConnection);
System.Data.SqlClient.SqlDataReader objSqlDataReader = objSqlCommand.ExecuteReader();
while(objSqlDataReader.Read())
{
strRecords = strRecords + "<Recordset>";
for(int i=0; i<objSqlDataReader.FieldCount; i++)
{
strRecords = strRecords + "<" + objSqlDataReader.GetName(i) + ">";
strRecords = strRecords + objSqlDataReader.GetValue(i);
strRecords = strRecords + "</" + objSqlDataReader.GetName(i) + ">";
}
strRecords = strRecords + "</Recordset>";
}
strRecords = strRecords + "</Root>";
objSqlDataReader.Close();
Close();
return strRecords;
}
catch(Exception e)
{
Close();
throw(e);
}
}
catch(Exception e)
{
throw(e);
}
}
/// <summary>
/// 获得数据库查询结果
/// 用于获取由 Page、Length 参数指定条件的查询结果
/// </summary>
/// <param name="Sql">数据库查询语句</param>
/// <param name="Page">数据库查询结果的页数</param>
/// <param name="Length">数据库查询结果的页长</param>
/// <returns>数据库查询结果</returns>
public string Records(string Sql,int Page,int Length)
{
try
{
Open();
try
{
string strRecords = "<?xml version=\"1.0\" encoding=\"gb2312\" ?><Root>";
Sql = "select top " + Page * Length + " * from (" + Sql + ") derivedtbl";
System.Data.SqlClient.SqlCommand objSqlCommand = new System.Data.SqlClient.SqlCommand(Sql,objSqlConnection);
System.Data.SqlClient.SqlDataReader objSqlDataReader = objSqlCommand.ExecuteReader();
long lngCount = 0;
while(objSqlDataReader.Read())
{
lngCount = lngCount + 1;
if(lngCount > (Page - 1) * Length)
{
strRecords = strRecords + "<Recordset>";
for(int i=0; i<objSqlDataReader.FieldCount; i++)
{
strRecords = strRecords + "<" + objSqlDataReader.GetName(i) + ">";
strRecords = strRecords + objSqlDataReader.GetValue(i);
strRecords = strRecords + "</" + objSqlDataReader.GetName(i) + ">";
}
strRecords = strRecords + "</Recordset>";
}
}
strRecords = strRecords + "</Root>";
objSqlDataReader.Close();
Close();
return strRecords;
}
catch(Exception e)
{
Close();
throw(e);
}
}
catch(Exception e)
{
throw(e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -