寻连接数据库方案 闹不明白.net怎么封装数据库操作的呀,java里可以将数据库连接封装在一个类中,然后在其后的文件里引用这个类就行了。不知道.net怎么弄呀,请高手指教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class DataAccessBase { private string _storedprocedureName; //使用的存储过程名称 protected string StoredProcedureName { get { return _storedprocedureName; } set { _storedprocedureName = value; } } //返回数据库的连接字符串 protected string ConnectionString { get { return ConfigurationManager.ConnectionStrings["SQLCONN"].ToString(); } } }连接字符串在配置文件中,连接到数据库的方式是把字符串传递给SqlHelper.cs。 配置连接字符串使用数据库操作类如sqlhelper看看petshop web.config: <!-- 连接字符串,(可以扩展支持不同数据库)如果是加密方式,上面一项要设置为true,如果是明文server=127.0.0.1;database=codematic;uid=sa;pwd=,上面设置为false --> <connectionStrings> <add name="ApplicationServices" connectionString="Data Source=.;Initial Catalog=xx;Integrated Security=True"/> </connectionStrings>SqlHelper.cs: protected string ConnectionString { get { return ConfigurationManager.ConnectionStrings["SQLCONN"].ToString(); } } 奥,这样啊,也就是说在web.config里写数据库连接,然后用类取得这个连接,在类里定义数据库的操作,然后在用到的时候引用这个类。是这样理解吧? 一样的,你JAVA都会了,C#也很容易。。C#就是JAVA的盗版,呵呵 SqlHelper.csusing System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;using System.Configuration;/// <summary>/// Summary description for SqlHelper/// </summary>public class SqlHelper{ string sqlconn = ConfigurationManager.ConnectionStrings["connStr"].ToString(); /// <summary> /// 返回集合类型的查询 也可以单个查询 /// </summary> /// <param name="sqlstr"></param> /// <param name="para"></param> /// <returns></returns> public DataTable DataRet(string sqlstr, SqlParameter[] para) { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(sqlconn)) { SqlCommand comm = new SqlCommand(); comm.Connection = conn; comm.CommandText = sqlstr; comm.Parameters.AddRange(para); SqlDataAdapter sa = new SqlDataAdapter(); sa.SelectCommand=comm; try { conn.Open(); sa.Fill(dt); conn.Close(); } catch (Exception ex) { conn.Close(); throw (ex); } finally { conn.Close(); } } return dt; } /// <summary> /// 返回执行是否执行成功 增 删 改 都可以用这个 /// </summary> /// <param name="sqlstr"></param> /// <param name="para"></param> /// <returns></returns> public int IntRet(string sqlstr, SqlParameter[] para) { int row = 0; using (SqlConnection conn = new SqlConnection(sqlconn)) { SqlCommand comm = new SqlCommand(); comm.Connection = conn; comm.CommandText = sqlstr; comm.Parameters.AddRange(para); try { conn.Open(); row = comm.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { conn.Close(); throw (ex); } finally { conn.Close(); } } return row; }}java不是由spring 托管了数据库连接么? c# 外部引用c++非托管DLL问题,十万火急,数据结构int32_t 如何使用SQL语句把多笔记录写入EXCEL中? c#字符串中怎麽插入“http://www.sina.com.cn” 请各位帮帮忙,先谢谢了! 如何实现程序的启动界面 简单的多线程问题! .aspx网页打不开 VTxtAuto的问题,请高手解决 c#里有没有类似delphi里窗体继承的概念. 关于组件自定义属性说明的问题,大家帮忙看一下 利用WMI或者PSexec远程启动一个程序后怎么没界面? VS2003 Calendar 显示汉字问题
public class DataAccessBase
{
private string _storedprocedureName;
//使用的存储过程名称
protected string StoredProcedureName
{
get { return _storedprocedureName; }
set { _storedprocedureName = value; }
}
//返回数据库的连接字符串
protected string ConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings["SQLCONN"].ToString();
}
}
}
连接字符串在配置文件中,
连接到数据库的方式是把字符串传递给SqlHelper.cs。
看看petshop
<!-- 连接字符串,(可以扩展支持不同数据库)如果是加密方式,上面一项要设置为true,如果是明文server=127.0.0.1;database=codematic;uid=sa;pwd=,上面设置为false -->
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=.;Initial Catalog=xx;Integrated Security=True"/>
</connectionStrings>SqlHelper.cs:
protected string ConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings["SQLCONN"].ToString();
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for SqlHelper
/// </summary>
public class SqlHelper
{
string sqlconn = ConfigurationManager.ConnectionStrings["connStr"].ToString();
/// <summary>
/// 返回集合类型的查询 也可以单个查询
/// </summary>
/// <param name="sqlstr"></param>
/// <param name="para"></param>
/// <returns></returns>
public DataTable DataRet(string sqlstr, SqlParameter[] para)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(sqlconn))
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = sqlstr;
comm.Parameters.AddRange(para);
SqlDataAdapter sa = new SqlDataAdapter();
sa.SelectCommand=comm;
try
{
conn.Open();
sa.Fill(dt);
conn.Close();
}
catch (Exception ex)
{
conn.Close();
throw (ex);
}
finally
{
conn.Close();
}
} return dt;
} /// <summary>
/// 返回执行是否执行成功 增 删 改 都可以用这个
/// </summary>
/// <param name="sqlstr"></param>
/// <param name="para"></param>
/// <returns></returns>
public int IntRet(string sqlstr, SqlParameter[] para)
{
int row = 0;
using (SqlConnection conn = new SqlConnection(sqlconn))
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = sqlstr;
comm.Parameters.AddRange(para);
try
{
conn.Open();
row = comm.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
conn.Close();
throw (ex);
}
finally
{
conn.Close();
} }
return row;
}}java不是由spring 托管了数据库连接么?