这就是所谓的多层架构里的数据链接层DLL了.
在某一个类里把连接数据库的方法封装,当然还有其他一些数据库操作的类都可以封起来.用的时候只要调用就可以.
在某一个类里把连接数据库的方法封装,当然还有其他一些数据库操作的类都可以封起来.用的时候只要调用就可以.
解决方案 »
- 超级菜的问题C#比较大小。。
- lua和C#一起用具体有什么应用呢?
- 请教关于grid控件,有兴趣的朋友请进来帮忙看看,谢谢了
- Windows里面的Settings.settings里面的问题
- c#写异步登录界面?指教指教
- 关于treeview中传参的问题
- AppLibrary.WriteExcel.XlsDocument 导出excel问题
- 关于System.EnterpriseServices.ServicedComponent的问题
- winform如何修改系统分辨率和刷新率,不调用API??
- winform主细表问题,主表可读出数据,明细表却没有数据
- 像windows纸牌那样在点发牌在下面显示,而不是用新建窗口的方法,各位帮帮忙!
- InvalidArgument=“-1”的值对于“index”无效
{
private const string m_strConnString = "Provider=microsoft.jet.oledb.4.0;data source=c:\\school.mdb"; public static OleDbConnection GetConnection()
{
return new OleDbConnection(m_strConnString);
}
} protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection conn = DbConnect.GetConnection();
conn.Open();
}如果只是达到楼主要求的功能的话,这样就可以了,但是这么做会把数据库的操作都放到表现层,未体现出分层的概念。
/// The SqlHelper class is intended to encapsulate high performance,
/// scalable best practices for common uses of SqlClient.
/// </summary>
public abstract class SqlHelper { //Database connection strings
public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["SQLConnString1"].ConnectionString;
public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;
public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;
// Hashtable to store cached parameters
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); /// <summary>
/// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string
/// using the provided parameters.
/// </summary>
/// <res>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </res>
/// <param name="connectionString">a valid connection string for a SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString)) {
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
} /// <summary>
/// Execute a SqlCommand (that returns no resultset) against an existing database connection
/// using the provided parameters.
/// </summary>
/// <res>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </res>
/// <param name="conn">an existing database connection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
} /// <summary>
/// Execute a SqlCommand (that returns no resultset) using an existing SQL Transaction
/// using the provided parameters.
/// </summary>
/// <res>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </res>
/// <param name="trans">an existing sql transaction</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
看看高手的对你有帮助他用WEB.CONFIG的读取
看看我的资源里有用Access做的东西。里面有源码。
{
private bool DBIsAlive; public SqlConnection Conn1;
public SqlCommand Cmd1;
public SqlDataReader sdr;
public string ErrMsg;
public string DBLocation, DBName, DBUser, DBPaswd;
private string con_str;
private int ConnectDB()
{
int reuslt = 1;//·µ»ØÖµ
if (DBLocation == "" || DBName == "" || DBUser == "" || DBPaswd == "")
return -1;
string ConStr = "uid=" + DBUser + ";password=" + DBPaswd + ";database=" + DBName + ";server=" + DBLocation;
Conn1 = new SqlConnection();
Conn1.ConnectionString = ConStr;
con_str = ConStr;
try
{
Conn1.Open();
DBIsAlive = true;
}
catch(Exception ee)
{
Conn1.Close();
DBIsAlive = false;
ErrMsg = ee.Message;
reuslt = -2; //Êý¾Ý¿âÁ¬½Óʧ°Ü
}
return reuslt;
} private int DisconnectDB()
{
int result = 1;
if (DBIsAlive)
{
try
{
Conn1.Close();
//Conn1.Dispose();
}
catch(Exception ee)
{
ErrMsg = ee.Message;
result = -3; //Êý¾Ý¿â¶Ï¿ªÊ§°Ü
}
}
else
{
try
{
//Conn1.Dispose();
Conn1.Close();
}
catch(Exception ee)
{
result = -3;
ErrMsg = ee.Message;
}
}
return result;
} public bool DB_Content_Test()
{
bool result = false;
if (ConnectDB() == 1)
result = true;
if (DisconnectDB() != 1)
result = false;
return result;
} public SqlDataReader SelectDetial(string command)
{
if (command != null)
{
ConnectDB();
Cmd1 = new SqlCommand();
Cmd1.CommandText = command;
Cmd1.Connection = Conn1;
sdr = Cmd1.ExecuteReader();
}
return sdr;
} public void UpdateDetial(string command)
{ if (command != null)
{
ConnectDB();
Cmd1 = new SqlCommand();
Cmd1.CommandText = command;
Cmd1.Connection = Conn1;
Cmd1.ExecuteNonQuery();
}
}
}