在SQLHelper有一句:
public static readonly string CONN_STRING = ConfigurationSettings.AppSettings["SqlConnString"];
是用WEB.config来连接数据库的~只要在config里的<appSettings>中写好连接字符串就OK了.现在我不想把web方式中访问数据库的配置放在web.config里,在c/s中我想调用SQLHelper.dll及以前开发WEB时编好的连接类读写数据库,不知怎样访问,配置文件怎么做,放在哪?SQLHelper中如何修改?请教各位,谢谢!
问题
public static readonly string CONN_STRING = ConfigurationSettings.AppSettings["SqlConnString"];
是用WEB.config来连接数据库的~只要在config里的<appSettings>中写好连接字符串就OK了.现在我不想把web方式中访问数据库的配置放在web.config里,在c/s中我想调用SQLHelper.dll及以前开发WEB时编好的连接类读写数据库,不知怎样访问,配置文件怎么做,放在哪?SQLHelper中如何修改?请教各位,谢谢!
问题
SqlConnection conn = new SqlConnection(SQLHelper.connstr);
conn.Open();
在相应的CS文件 引入类库 using System.Configuration
在添加项目app.config 在
<configuration></configuration> 中添加<appSettings></appSettings> 然就就不用我说的吧!不知道 你是不是想要的!
我的CS里是这样写的
public string Foodqs
{
get
{
return SqlConnString;
}
set
{
switch(value)
{
case "getconn":
SqlConnString = "SERVER=(local);uid=;pwd=;DATABASE=";
break;
default:
break;
}
}
}
我当然知道 在效果页上
SqlConn DBconn = new SqlConn();
DBconn.cn = "数据库名";
这么一下就能连上数据库了,这是我以前写的 我现在就是想能在SQLHelper里读取到我的这个CS连接类,
按我的理解是要修改CONN_STRING这个数据库字符串,但是我不明白怎么样才能把我的
SqlConnString = "SERVER=(local);uid=;pwd=;DATABASE=";
字符串赋值到SQLHelper里面?
然后在sqlhelp里写这个
protected static string connstr = ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;
在类里进行引用
SqlConnection conn = new SqlConnection(SQLHelper.connstr);
conn.Open();
(也可以用别的引用方式进行连库)
public static readonly string CONN_STRING = "连接字符串"
我有一个getconn.cs是专门连接数据库的类.现在就是想在SQLHelper里调用我的getconn.cs这个类,请问要怎么做啊?
在你要用的地方用引用sqlhelp.con
这样就可以了.
而是怎么才能在我的SQLHelper中调用我以前写好的连接类....]
SQLHELPER是我下载的很有名一个封装执行类.
get
{
return SqlConnstr;
}
set
{
switch(value)
{
case "realty":
SqlConnstr = "SERVER=(local);uid=sa;pwd=;DATABASE=xget"; //OA库
break;
default:
break;
}
}
} public string SqlConnstring() //定义一个方法:返回连接字符串
{
return SqlConnstr;
} public string ReturnValue(string sql)
{
string returnValue = "";
SqlConnection Conn=new SqlConnection(SqlConnstr);
SqlCommand My_cmd=new SqlCommand(sql,Conn);
Conn.Open();
SqlDataReader dr=My_cmd.ExecuteReader();
if (dr.Read())
{
returnValue = dr["vluer"].ToString();
}
dr.Close();
Conn.Close();
Conn.Dispose();
return returnValue;
}
连接大概就是这样.
然后我外面以前是这样的:
SqlConn DBconn = new SqlConn();
DBconn.Foodqs = "realty";
// truename.Text = DBconn.ReturnValue("select name as vluer from work_personnel");
这样就能输出了.哥们你看看有什么好办法吗?
不知道楼主是这个意思不?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SQLConnString" value="uid=...;PWD=...;DATA SOURCE=...;INITIAL CATALOG=.." />...
</appSettings>
</configuration>又想用SQLHelper.DLL
那么只有:自己写个数据访问层程序集或者直接写在类里
string SQLConnString="....." //链接字符串
string sql="...";
SqlConnection Conn = new SqlConnection(SQLConnString);
SqlCommand Com =new SqlCommand(sql,Conn);
Conn.Open();
public string fconns {
get
{
return SqlConnstr;
}
set
{
switch(value)
{
case "realty":
SqlConnstr = "SERVER=(local);uid=sa;pwd=;DATABASE=xget"; //OA库
break;
default:
break;
}
}
}
public string SqlConnstring()
{
return SqlConnstr;
}
然后在SQLHelper里改为
SqlConn DBconn = new SqlConn();
public string CONN_STRING =SqlConn.SqlConnstring();
还是出错:
非静态的字段、方法或属性“getconn.SqlConn.SqlConnstring()”要求对象引用
public static string SqlConnstring()
{
return SqlConnstr;
}
string CONN_STRING =getconn.SqlConn.SqlConnstring(); 2.将getconn作为基类
public class SQLHelper:getconngetconn里的方法要为PROTECED或PUBLIC
这样就什么直接可以用了
既然string CONN_STRING这个没有办法传值就直接去掉.保留SQLHELPER的其它功能
然后在前台所有页面调用时就直接把我自己写的那个类当做参数带进来~!我太笨了....一门的想在
SQLHELPER里面定义..感觉大家帮助!