Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从c#应用程序连接到MYSQL_C#_.net_Mysql - Fatal编程技术网

从c#应用程序连接到MYSQL

从c#应用程序连接到MYSQL,c#,.net,mysql,C#,.net,Mysql,项目上的基础数据库已从sql 2005更改为MySql 5.1 代码中有许多类似于下面的方法。我认为这只是将'con'变量SqlConnection切换到MYSql特定连接的一种情况。有人有过这方面的经验吗?我从未接触过mySql数据库。非常感谢您的帮助 private SqlConnection con; public User LogonUser(string pUserName, string pPassword) { con = new SqlConn

项目上的基础数据库已从sql 2005更改为MySql 5.1 代码中有许多类似于下面的方法。我认为这只是将'con'变量SqlConnection切换到MYSql特定连接的一种情况。有人有过这方面的经验吗?我从未接触过mySql数据库。非常感谢您的帮助

private SqlConnection con;

    public User LogonUser(string pUserName, string pPassword)
    {

        con = new SqlConnection();
        con.ConnectionString = DatabaseConstants.DB_CONN_STRING;


        using (con)
        {
            con.Open();
            var command = new SqlCommand();


            command.Connection = con;
            command.CommandText = "SELECT id FROM Users WHERE userName = @userName AND password = @password";
            command.CommandType = CommandType.Text;

            var userName = new SqlParameter("@userName", pUserName);
            var password = new SqlParameter("@password", pPassword);

            command.Parameters.Add(userName);
            command.Parameters.Add(password);


            User user;
            var dr = command.ExecuteReader();
            if (dr != null)

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        user = new User();
                        user.id = dr.GetString(0);
                        return user;
                    }
                }
                else
                {
                    throw new Exception("Can not find user, please check your username and password");
                }
        }
        return null;
    }

部分正确,但需要MySQL提供程序的实例,而不是SqlConnection。此外,您还必须更改任何与MySQL不兼容的SQL。

可下载的SQL连接器可用于各种框架和平台-在本例中,以ADO.NET的名义引用到.NET项目中的程序集是。可以使用任何.NET语言编写针对它们的程序

从C#开始,引用MySql名称空间:

using MySql.Data.MySqlClient; 

并将ADO.NET类名从
SqlConnection
更改为
MySqlConnection
,等等。显示粗略的用法(与其他ADO.NET提供程序类似),当然。

不,您还必须更改此行

var command = new SqlCommand();

当然,您必须将任何特定的T-SQL和MSSQL特性更改为MySQL。日期和时间函数、存储过程和参数绑定(?而不是@)是需要仔细检查的几件事情

var command = new con.CreateCommand();