通常我使用这个类来创建一个sqlconnection:
namespace IntraFaz
{
class Tconex
{
public static SqlConnection GetConnection()
{
//string str = "Data Source=SQL-PC;Initial Catalog =ifdcontroladoria3;uid =sa;pwd = admin2012";
string str = "Data Source=300.161.430.110 ;Initial Catalog =Pirulim;uid =sa;pwd = per#3";
SqlConnection con = new SqlConnection(str);
con.Open();
return con;
}
}
}
我在表单中调用此类以使用此
创建新的sqlconnectionSqlConnection conec1 = Tconex.GetConnection();
我需要写这个类来获得两个我想要的东西
public static SqlConnection GetConnection()
{
string str = "Data Source=300.161.430.110 ;Initial Catalog =Pirulim;uid =sa;pwd = per#3";
string str2 = "Data Source=300.161.430.112 ;Initial Catalog =Pirulim;uid =sa;pwd = per#4";
SqlConnection con = new SqlConnection(str);
con.Open();
SqlConnection con = new SqlConnection(str2);
con.Open();
return con;
}
答案 0 :(得分:1)
public static SqlConnection GetConnection()
{
string str = "Data Source=300.161.430.110 ;Initial Catalog =Pirulim;uid =sa;pwd = per#3";
SqlConnection con = new SqlConnection(str);
con.Open();
return con;
}
public static SqlConnection GetConnection2()
{
string str2 = "Data Source=300.161.430.112 ;Initial Catalog =Pirulim;uid =sa;pwd = per#4";
SqlConnection con = new SqlConnection(str2);
con.Open();
return con;
}
答案 1 :(得分:1)
public static SqlConnection[] GetConnections()
{
SqlConnection con1 = new SqlConnection(str1);
SqlConnection con2 = new SqlConnection(str2);
con1.Open();
con2.Open();
return new SqlConnection[] { con1, con2 };
}
答案 2 :(得分:0)
public static Tuple<SqlConnection, SqlConnection> GetTwoConnections()
{
...
SqlConnection con1 = new SqlConnection(str1);
SqlConnection con2 = new SqlConnection(str2);
con1.Open();
con2.Open();
return Tuple.Create(con1, con2);
}
用法:
var cons = GetTwoConnections();
using (cons.Item1, cons.Item2)
{
// use first connection as cons.Item1 and second one as cons.Item2
}
答案 3 :(得分:0)
创建一个类来保存方法的结果返回。
public class TheseConnectinsHaveThisPurpose
{
public SqlConnection ThisConnectionHasThisPurpose { get; set; }
public SqlConnection ThisConnectionHasThisOtherPurpose { get; set; }
}
答案 4 :(得分:0)
您的连接唯一不同的是密码,所以:
public static SqlConnection GetConnection(string pass)
{
string str = "Data Source=300.161.430.110 ;Initial Catalog =Pirulim;uid =sa;pwd ="+pass;
SqlConnection con = new SqlConnection(str);
con.Open();
return con;
}
稍后实现如果其他参数改变(例如userid)你可以将它作为参数添加到你的方法......