如何根据查询选择修改连接字符串
how to modify connection string based on query selection
我有一个 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);
}
}
这里是使用 table 名称更改我的数据库的查询,需要修改连接:
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 ";
如何根据查询选择以编程方式执行此操作?
伙计们,请帮帮我
我强烈建议您使用 .NET 框架中已有的 SqlConnectionStringBuilder
class。这允许您指定连接字符串的各种属性,并确保您获得有效的连接字符串。
像这样:
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;
使用这种方法,您可以根据需要以任何方式调整连接字符串 - 只需根据您的条件设置一些属性即可。
我有一个 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);
}
}
这里是使用 table 名称更改我的数据库的查询,需要修改连接:
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 ";
如何根据查询选择以编程方式执行此操作?
伙计们,请帮帮我
我强烈建议您使用 .NET 框架中已有的 SqlConnectionStringBuilder
class。这允许您指定连接字符串的各种属性,并确保您获得有效的连接字符串。
像这样:
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;
使用这种方法,您可以根据需要以任何方式调整连接字符串 - 只需根据您的条件设置一些属性即可。