我有一个Web项目,需要根据查询执行来更改连接字符串。这是我在以下代码中定义连接字符串的代码:
protected void GetConnection(string cnn_db = "NEWBULKSMS")
{
try
{
string Cnn_Str = "";
//string ServerName = "SERVER2008\\SQL_EXP_2008";
//string DBUserName = "sa";
//string DBPassword = "sa_123";
string ServerName = "SHREE-PC";
string DBUserName = string.Empty;
string DBPassword = string.Empty;
DBPassword += "c#" + Convert.ToChar(49);
string Database = cnn_db;
Cnn_Str = "Data Source=" + ServerName + "; UID=" + DBUserName + "; PWD=" + DBPassword + "; Database=" + Database+";Integrated Security = True";
//Cnn_Str = "Data Source=SHREE-PC;Initial Catalog=Project_DB_MNG;Integrated Security=True";
mstr_ConnectionString = Cnn_Str;
mobj_SqlConnection = new SqlConnection(mstr_ConnectionString);
mobj_SqlCommand = new SqlCommand();
mobj_SqlCommand.CommandTimeout = mint_CommandTimeout;
mobj_SqlCommand.CommandType = CommandType.StoredProcedure;
mobj_SqlCommand.Connection = mobj_SqlConnection;
mobj_SqlConnection.Open();
}
catch (Exception ex)
{
throw new Exception("Error initializing data class." + Environment.NewLine + ex.Message);
}
}
这是使用表名更改我的数据库的查询,该表名需要修改连接:
str = "SELECT ROW_NUMBER() OVER (ORDER BY RowId DESC) AS RowNumber, p.CampaignName,";
str += "p.MobileNo,";
str += "p.Message,";
str += "p.CharCount,";
str += "p.strSenderID AS Sender,";
str += "u.strUserName AS UserId,";
str += "ds.strDR AS DeliveryStatus,";
str += "ds.strDiscription AS Original_DRStatus,";
str += "m.strMessageType AS MessageType,";
str += "CONVERT(VARCHAR(20), p.ReceiveTime) AS ReceiveTime,";
str += "CONVERT(VARCHAR(20), p.SendTime) AS SendTime,";
str += "CONVERT(VARCHAR(20), p.DoneTime) AS DoneTime,";
str += "p.RootId AS Root,";
str += "sp.ProviderName,";
str += "(CASE intAccountType WHEN 1 THEN 'Promotional' WHEN 2 THEN 'Transactional' WHEN 3 THEN 'OptIn' END) as AccountType";
str += " INTO ##Results3 ";
//str += " FROM NEWSMSLOG_2019_01..LOG_010119 p "; //here I want to change connection string with another database
str += " FROM NEWBULKSMS.dbo.LOG_010119 p ";
str += " INNER JOIN deliverstatus ds ON p.DeliveryStatus = ds.intDR_status inner join users u on u.id = p.userid";
str += " LEFT JOIN senderids b ON b.id = p.senderid";
str += " LEFT JOIN messagetype m ON m.intcode = p.messagetype";
str += " LEFT JOIN smppproviders sp ON sp.RootId = p.RootId";
str += " WHERE 1 = 1 ";
如何基于查询选择以编程方式实现它?
请帮助我,伙计们
答案 0 :(得分:1)
我会强烈建议,您使用.NET框架中预先存在的SqlConnectionStringBuilder
类。这样,您就可以指定连接字符串的各种属性,并确保获得有效的连接字符串。
类似这样的东西:
string ServerName = "SHREE-PC";
string Database = cnn_db;
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
// set the properties
scsb.DataSource = ServerName;
scsb.InitialCatalog = Database;
// optionally set a user name / password
scsb.UserId = "YourUserName";
scsb.Password = "top$ecret";
// or if you leave out the username/password - set the "Integrated Security" flag instead
scsb.IntegratedSecurity = true;
// possibly set other options here....
// finally - get your connection string
string connectionString = scsb.ConnectionString;
使用这种方法,您可以根据需要任意调整连接字符串-只需根据您的条件设置一些属性即可。