为我的 C# .NET Winform 应用程序创建单个连接文件

发布于 2024-11-17 08:19:10 字数 402 浏览 1 评论 0原文

我想创建一个连接文件,使用该文件,我的 winform 应用程序的所有表单都应连接到在线 mysql 数据库并选择、更新和保存。插入数据。

我已将连接文件命名为 CONNECTION.CS,连接字符串为:

OdbcConnection conn = new OdbcConnection("Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306"); 

现在,我如何在 Form1.cs、Form2.cs 中使用它......建立与数据库的连接并开始插入并检索数据?请帮忙。

我需要在所有表单中继承这个Connection.cs吗?请帮忙看一下代码

I want to make a single connection file , using which , all the Forms of my winform application should connect to the online mysql database and select,update & insert data.

I have named the connection file as CONNECTION.CS, connection string is :

OdbcConnection conn = new OdbcConnection("Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306"); 

Now , how do I use it in the Form1.cs,Form2.cs ..........to establish connection to the database and start inserting and retreiving data? Please help.

Do i need to inherit this Connection.cs in all the Forms? Please help with code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2024-11-24 08:19:10

我认为如果您在 app.config 文件中定义它会更容易

<appSettings>
  <add key="ConnectionString" value="Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306" />
   </appSettings>
</configuration>

,因此每当您想要获取连接字符串时都可以获取它:

string strConn = ConfigurationManager.AppSettings["ConnectionString"];

或者您可以使用类作为数据访问层:

class Connection
{
     OleDbConnection conn;
     OleDbCommand cmd;
     public Connection()
     {
          string connnstr = "Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306";
          conn = new OleDbConnection(connstr);
          cmd = new OleDbCommand();
          cmd.Connection = conn;
     }
     public OleDbDataReader GetData()
     {
        ....
     }
}

然后每当您想通过

Connection conn = new Connection();
OleDbDataReader dr = conn.getData();

此 获取数据时您只需定义一个连接文件即可。

I think it will be easer if you define it in the app.config file

<appSettings>
  <add key="ConnectionString" value="Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306" />
   </appSettings>
</configuration>

so whenever you want to get a connectionstring you can get it :

string strConn = ConfigurationManager.AppSettings["ConnectionString"];

or you can use a class as a data access layer :

class Connection
{
     OleDbConnection conn;
     OleDbCommand cmd;
     public Connection()
     {
          string connnstr = "Driver={MySQL ODBC 5.1 Driver};uid=ab ; password=pass;server=www.myweb.com;database=mydb;port=3306";
          conn = new OleDbConnection(connstr);
          cmd = new OleDbCommand();
          cmd.Connection = conn;
     }
     public OleDbDataReader GetData()
     {
        ....
     }
}

then whenever you want to getdata just

Connection conn = new Connection();
OleDbDataReader dr = conn.getData();

by this way you just define a single connection file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文