使用dot Net Windows Apps远程连接MySQL数据库 [英] Connect MySQL Database Remotely with dot Net Windows Apps

查看:77
本文介绍了使用dot Net Windows Apps远程连接MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为商店的零售管理开发窗口应用程序。数据库安装在服务器中,但应用程序安装在不同的计算机上,无法连接数据库。

Plz告诉我确切的代码......

i我正在尝试..

  string  strCon =   server = 198.168.1.162; Database = inventory; UserID = root; Password = root; 
MySqlConnection con = new MySqlConnection(GlobalConectionClass.FnConnection());
private void FillDdl()
{
尝试
{
字符串 QrySuppType = 按供应商类型选择*按类型asc;
// MySqlDataAdapter adpQrySuppType = new MySqlDataAdapter(QrySuppType,con);
< span class =code-comment> // DataSet dsQrySuppType = new DataSet();
MySqlCommand cmd1 = new MySqlCommand(QrySuppType,con);
con.Open();
MySqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
ddlSuppType.Items.Add(dr1 [ type]。ToString());
ddlSearchST.Items.Add(dr1 [ type]。ToString());
ddlRemoveSupType.Items.Add(dr1 [ type]。ToString());
}
dr1.Close();
}
catch
{
}
finally
{
con.Close();
}
}

解决方案

在服务器系统上的mysql服务器中创建新用户并允许访问库存数据库到该用户,然后更改代码中的用户名和密码。无需更改代码


此代码由VB.NET 2008& MySQL



步骤1



使用以下代码创建一个模块。



  Imports  MySql.Data.MySqlClient 
模块 mod_databaseconnection

公共 cn 作为 MySqlConnection
公共 cmd 作为 MySqlCommand
Public dr As MySqlDataReader
Public Sub open_clienconnection()
尝试
cn = MySqlConnection
cn.ConnectionString = server = 192.168.232.11 ;用户ID = r oot; password = 4; Persist Security Info = True; database = test
cn.Open()
cmd.Connection = cn
Catch ex As 异常
MessageBox.Show( 发生意外的数据库连接错误。& vbCrLf& ex.Message, 注意,MessageBoxButtons.OK,MessageBoxIcon。错误

结束 尝试
结束 Sub

结束 模块





第2步



在每个sql查询中你都会执行确保调用open_clienconnection()





代码将以某种方式看起来像这样



 open_clienconnection()
cmd.CommandText = select * from MyTable
dr = cmd.ExecuteReader
dr.Read()





如果有效,请告诉我。


您好,



请在MySQL上启用TCP / IP网络。默认情况下,在安装MySQL服务器时未启用此选项。还要确保从远程计算机授予mysql数据库用户访问权限。您可以通过发出以下命令来执行此操作。默认情况下,root不允许从localhost以外的机器连接,即安装MySQL服务器的机器。

  GRANT  priv_type  ON  db_name。*  TO  user_name @ remote_host 



注释 -

  • 此处小写的值表示您需要根据用户数据库和用户更改的值
  • 有关 GRANT 命令的更多帮助,请查看MySQL文档


问候,

Developing a Window apps for Retail Management of a Store. The database is installed in a Server but application is installed to different Computers and Unable to connect Database.
Plz tell me exact code ....
i am trying..

string strCon = "server =198.168.1.162 ;Database=inventory;UserID=root;Password=root";
 MySqlConnection con = new MySqlConnection(GlobalConectionClass.FnConnection());
private void FillDdl()
        {
            try
            {                
                string QrySuppType = "Select * from supplier_type order by type asc";
                //MySqlDataAdapter adpQrySuppType = new MySqlDataAdapter(QrySuppType, con);
                //DataSet dsQrySuppType = new DataSet();
                MySqlCommand cmd1 = new MySqlCommand(QrySuppType,con);
                con.Open();
                MySqlDataReader dr1 = cmd1.ExecuteReader();
                while (dr1.Read())
                {
                    ddlSuppType.Items.Add(dr1["type"].ToString());
                    ddlSearchST.Items.Add(dr1["type"].ToString());
                    ddlRemoveSupType.Items.Add(dr1["type"].ToString());
                }
                dr1.Close();
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }

解决方案

create new user in mysql server on server system and allow the access to the inventory database to that user and then change the username and password in your code. no need to change the code


This code is made out of VB.NET 2008 & MySQL

Step 1

Create a Module with the following code.

Imports MySql.Data.MySqlClient
Module mod_databaseconnection

    Public cn As MySqlConnection
    Public cmd As New MySqlCommand
    Public dr As MySqlDataReader
    Public Sub open_clienconnection()
        Try
            cn = New MySqlConnection
            cn.ConnectionString = "server=192.168.232.11;User Id=root;password=4;Persist Security Info=True;database=test"
            cn.Open()
            cmd.Connection = cn
        Catch ex As Exception
            MessageBox.Show("An unexpected database connection error occured." & vbCrLf & ex.Message, "Attention", MessageBoxButtons.OK, MessageBoxIcon.Error)
          
        End Try
    End Sub

End Module



Step 2

In every sql query you will execute make sure to call "open_clienconnection()"


Code will somehow look like this

open_clienconnection()
            cmd.CommandText = "select * from MyTable"
            dr = cmd.ExecuteReader
            dr.Read()



Let me know if this works.


Hello,

Please enable TCP/IP networking on the MySQL. By default this option is not enabled while you install the MySQL Server. Also make sure that the mysql database user is granted access from the remote machine. You can do this by issuing following command. By default root is not allowed to connect from machine other than the localhost i.e. the machine on which MySQL server is installed.

GRANT priv_type ON db_name.* TO user_name@remote_host


Notes -

  • Here values in small case denotes the values you will need to change with respect to user database and user
  • For more help on GRANT command have a look at MySQL Documentation

Regards,


这篇关于使用dot Net Windows Apps远程连接MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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