如何从一个类文件访问连接字符串到另一个类文件 [英] how to access connection string from one class file to another class file
问题描述
在app_code中,有两个文件夹
1.连接
2.登录
连接类管理多个连接字符串
代码为: -
public class 连接
{
string conn;
SqlConnection con;
public 连接( string val)
{
if (val == 1)
{
conn = ConfigurationManager.ConnectionStrings [ connectionString]。ConnectionString;
}
else if (val == 2)
{
conn = ConfigurationManager.ConnectionStrings [ connectionString1]。ConnectionString;
}
// TODO:在这里添加构造函数逻辑
con = new SqlConnection(conn);
}
和登录类代码: -
< pre lang =c#> public class 登录
{
public 登录()
{
}
public string isLogin()
{
Connection clsConn = new Connection(< span class =code-string> 2);
SqlCommand cmd = new SqlCommand( login ,clsConn);
}
}
这里我不是在sqlcommand中访问clsconn所以任何人都告诉我如何访问???请
http://msdn.microsoft.com/en-us/library/877h0y3a(v = vs.110).aspx [ ^ ]
说 SqlCommand构造函数:
public SqlCommand(
string cmdText,
SqlConnection连接
)
因此第二个构造函数参数必须是 SqlConnection类的对象 。
但是你的第二个构造函数参数是一个完全不同类的对象。
这不行!
您必须更改以下代码:
SqlCommand cmd = new SqlCommand( login,clsConn.con);
确保 con 是公开的。
public SqlConnection con;
更改您的Connection类如下所示获取连接
public class 连接
{
public static SqlConnection GetConnection ( string val)
{ string conn = ;
if (val == 1 )
{
conn = ConfigurationManager.ConnectionStrings [ connectionString]的ConnectionString;
}
else if (val == 2)
{
conn = ConfigurationManager.ConnectionStrings [ connectionString1]。ConnectionString;
}
return new SqlConnection(conn);
}
}
然后你可以在其他课程中做到如下
public string isLogin()
{
SqlConnection clsConn = Connection.GetConnection( 2);
SqlCommand cmd = new SqlCommand( login ,clsConn);
}
我认为你需要
SqlCommand cmd =新的SqlCommand(login,clsConn .con );
由于你的Connection没有从sqlConnection继承,你必须得到连接来自你的Connectio课程的对象。
我建议你制作公共财产Connection而不是直接访问变量。
如果这有帮助,请花时间接受解决方案。谢谢。
in app_code, there are two folder
1. Connection
2. Login
connection class manage multiple connection string
code is:-
public class Connection
{
string conn;
SqlConnection con;
public Connection( string val)
{
if (val == "1")
{
conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
}
else if (val == "2")
{
conn = ConfigurationManager.ConnectionStrings["connectionString1"].ConnectionString;
}
// TODO: Add constructor logic here
con = new SqlConnection(conn);
}
and login class code :-
public class Login
{
public Login()
{
}
public string isLogin()
{
Connection clsConn= new Connection("2");
SqlCommand cmd = new SqlCommand("login", clsConn );
}
}
here i am not access clsconn in sqlcommand so anyone tell me how can i access ??? please
http://msdn.microsoft.com/en-us/library/877h0y3a(v=vs.110).aspx[^]
says about SqlCommand Constructor:
public SqlCommand( string cmdText, SqlConnection connection )
Therefore second constructor parameter must be an object of SqlConnection class.
But your second constructor parameter is an object of a completely different class.
This cannot work!
You must change your code like this:
SqlCommand cmd = new SqlCommand("login", clsConn.con);
Make sure that con is public.
public SqlConnection con;
change your Connection class to get connection as below
public class Connection { public static SqlConnection GetConnection( string val) { string conn =""; if (val == "1") { conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; } else if (val == "2") { conn = ConfigurationManager.ConnectionStrings["connectionString1"].ConnectionString; } return new SqlConnection(conn); } }
then you can do as below in other classes
public string isLogin() { SqlConnection clsConn= Connection.GetConnection("2"); SqlCommand cmd = new SqlCommand("login", clsConn); }
I think you need
SqlCommand cmd = new SqlCommand("login", clsConn.con );
Since your Connection does not inherit from sqlConnection, you have to get the connection object from your Connectio class.
I would suggest you make public property Connection instead of accessing the variable directly.
If this helps please take time to accept the solution. Thank you.
这篇关于如何从一个类文件访问连接字符串到另一个类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!