如何使应用程序动态获取数据库连接字符串? [英] How to make application get database connection string dynamically?

查看:92
本文介绍了如何使应用程序动态获取数据库连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,通常我将连接字符串放在App.config中并调用它来创建通常是静态的连接。



但是如果我想让这个连接字符串动态地改变,那么当我将应用程序放在另一台计算机上时,我只需使用表单输入数据库信息或调用它们来自类甚至文件,以使其易于更改,以便最好的方法是什么,以及如何在执行此操作后获取连接字符串。

解决方案

< blockquote>存储在文件中的想法也就是这样。

您只需要更改app文件中的配置,并且您的连接将对另一个数据库起作用。


< blockquote>我的项目中的代码我定义了一个发送命令的方法

你可以分析这个方法



  public   static   bool  SendCommand( string  IP, string  DatabaseName, string  UserName, string 密码, string  SqlQueryScript)
{
bool result = false ;
try
{
SqlConnection connection = new SqlConnection( 数据源= + IP + < span class =code-string>; Initial Catalog = + DatabaseName + ;用户ID = + UserName + ; Password = + Password);
string [] commandTextArray = SqlQueryScript.Split( new string [] { GO},StringSplitOptions.RemoveEmptyEntries);
connection.Open();
foreach string commandText in commandTextArray)
{
if (commandText.Trim()== string .Empty)继续;
SqlCommand command = new SqlCommand(commandText,connection);
command.ExecuteNonQuery();
connection.InfoMessage + = new SqlInfoMessageEventHandler(myConnection_InfoMessage);
}
connection.Close();


result = true ;
}
catch (例外情况)
{

ErrorMessage.errorMessage = ex.Message + < span class =code-string> \ n( + DateTime.Now.ToLongTimeString()+ ;
}
返回结果;
}


Hello, usually I put the connection string in App.config and call it to create connection which is usually static.

But what if I want to make this connection string dynamically changeable, so that when I put the application on another computer I just use a form to input database information or call them from from a class or even file, to make it changeable easy so what is the best way to do that, and how to get the connection string after doing that.

解决方案

The idea behind storing in a file is also just that.
You only need to change configuration in the app file and your connection will work against another database.


This code in my project I defined a method for send command
You can analyse this method

public static bool SendCommand(string IP, string DatabaseName, string UserName, string Password, string SqlQueryScript)
       {
           bool result = false;
           try
           {
           SqlConnection connection = new SqlConnection("Data Source=" + IP + ";Initial Catalog=" + DatabaseName + ";User ID=" + UserName + ";Password=" + Password);
           string[] commandTextArray = SqlQueryScript.Split(new string[] { "GO" }, StringSplitOptions.RemoveEmptyEntries);
           connection.Open();
           foreach (string commandText in commandTextArray)
           {
               if (commandText.Trim() == string.Empty) continue;
               SqlCommand command = new SqlCommand(commandText, connection);
               command.ExecuteNonQuery();
               connection.InfoMessage += new SqlInfoMessageEventHandler(myConnection_InfoMessage);
           }
           connection.Close();


           result = true;
            }
           catch(Exception ex)
           {

               ErrorMessage.errorMessage = ex.Message + "\n (" + DateTime.Now.ToLongTimeString()+")";
           }
           return result;
       }


这篇关于如何使应用程序动态获取数据库连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆