开发者

Taking the connection string value from textbox

In one class I have defined the connection string like this

  SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
            objConnectionString.DataSource = localServer; ;
            objConnectionString.UserID = userName;
            objConnectionString.Password = password;
            objConnectionString.InitialCatalog = selectedDatabase;

where local server = txtHost;--DataSource userName = txtUsername; password = txtPassword;

But in my another project I want to access the controls of that project

Currently I am connected with the db like this

  using(var sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"开发者_StackOverflow中文版]))

But I want to make it such that it would take the value directly from the textboxes used in another project

Waiting for your suggestions .....Can It be done..


You will not bne abble to do this, unless you pass the actual controls to the method in the other project.

Why not rather pass the SqlConnectionStringBuilder object that you set up before hand to the method being called?


In your form with the textbox you will need to create Properties to access the values from the form, e.g.

public string Server 
{
   get
   {
      return this.txtHost.Text;
   }
}

You will also need to pass a reference of the Form to your other Project, either by referencing the project, or using an shared interface between the two.

In your project where you want to build the connection string, you will need some way of receiving the reference to the Form, such as

public void RunMyQuery(MyForm form)
{
   var objConnectionString = new SqlConnectionStringBuilder();
   objConnectionString.DataSource = form.Server; 
}

If you have time, consider creating a new project which contain shared interfaces, so you could create an interface such as

public interface IConnectionStringPartProvider
{
   string Server { get; }
   ... other parts
}

and implement this interface on your form

public partial class Form1 : Form, IConnectionStringPartProvider

Then you would not need to reference you form project in your logic class, just let both projects reference the shared project.

This way, your query method could be replaced with

public void RunMyQuery(IConnectionStringPartProvider provider)
{
   var objConnectionString = new SqlConnectionStringBuilder();
   objConnectionString.DataSource = provider.Server; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜