在wpf中可能只有一次mysql数据库连接吗? [英] Is it possible mysql database connection in wpf only one time?

查看:71
本文介绍了在wpf中可能只有一次mysql数据库连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

在我的项目中,我编写了如下所示的连接.

hi friends,

In my project i write a connection like below,.

string MyConString = "SERVER=192.168.1.100;" + "DATABASE=mcs_kiosk;" + "UID=root;" + "PASSWORD=;";
                  MySqlConnection connection = new MySqlConnection(MyConString);
                  MySqlCommand command = connection.CreateCommand();
                  MySqlDataReader Reader;
                  command.CommandText = "select * from mcs_hall";
                  connection.Open();
                  Reader = command.ExecuteReader();
                  while (Reader.Read())
                  {
                      txt_Code.Text = Reader[1].ToString();
                      txt_Name.Text = Reader[2].ToString();
                  }
                  connection.Close();



但是这种类型的连接负载的次数,.所以我想要,

1.在xml或文本文件中进行连接,以更改MyConString.

2.程序只能读取一次连接.

3.在执行查询之前,连接要检查连接可用性.如果连接打开则执行查询.

这个怎么做,.或给我任何其他想法.

谢谢你.



but this type connection load number of time,. so i want,

1. connection in xml or text file, for change MyConString.

2. program can read only one time the connection.

3. before the executing query, the connection want to check connection availability. if connection open execute query.

how to do this,. or give me any other idea.

thank u.

推荐答案

只有几点想法...

将您的连接字符串放入App.Config:

Just few thoughts...

Put your connection string into to App.Config:

<connectionStrings>
  <add name="mysql"

    connectionString="Server=192.168.56.101;Database=mytest;Uid=mytest;Pwd=mytest"

    providerName="MySql.Data.MySqlClient" />
</connectionStrings>



您可以像这样初始化连接:



You can initialize your connection like this:

ConnectionStringSettings connectString = ConfigurationManager.ConnectionStrings[connectionString];

if (connectionString == null)
    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DatabaseServiceMessages.ConnectionStringNotFound, connectionString));

_factory = DbProviderFactories.GetFactory(connectString.ProviderName);
if (_factory == null)
    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DatabaseServiceMessages.ConnectionStringNotFound, connectString.ProviderName));

_connection = _factory.CreateConnection();
_connection.ConnectionString = connectString.ConnectionString;
_connection.Open();




将所有内容封装到静态类中(可以使用单例设计模式):




Encapsulate everything to a static class (you can use singleton design pattern):

public static class DbConnectionWrapper
{
  private static DbConnection _connection;

  public static DbConnection Connection
  {
    get 
    {
      if (_connection == null)
      {
      // setup new connection
      }
      else
      {
      // verify connection
      }
      return _connection;
    }
  }
}


这篇关于在wpf中可能只有一次mysql数据库连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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