C#MySQL连接状态 [英] C# mysql connection state

查看:209
本文介绍了C#MySQL连接状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.

我试图获取mysql连接状态,但不起作用.
我使用的代码:

I have a problem.

I tried to get a mysql connection status, but it doesn''t work.
The code i used:

if (conn.State != System.Data.ConnectionState.Open)
{
    try
    {
        string host = "localhost";
        string database = "proiectps";
        string username = "root";
        string password = "";
        string myConnString = "SERVER=" + host + ";DATABASE=" + database + ";UID=" + username + ";PASSWORD=" + password;
        conn = new MySqlConnection(myConnString);
        conn.Open();
    }
    catch (MySqlException e)
    {
        Console.WriteLine("Eroare de conectare MySQL. Eroare: " + e.ToString());
    }



我也尝试了
conn.state!= ConnectionState.Open

什么都没有工作.有人可以帮我吗?



and i also tried with
conn.state!= ConnectionState.Open

Nothing seams to work. Can anyone help me?

推荐答案

api_id写道:

这是一个例外,它向我显示:System.NullRefereceException:对象引用未设置为对象的实例.

This is the exception that it shows me: System.NullRefereceException: Object reference not set to an instance of an object.



基于此,给定的代码看起来像"conn"对象为null.

先尝试类似的方法,然后再进行其他操作:



Based on this, and the given code it looks like ''conn'' object is null.

Try something like this first and then move on with other stuff:

void BtnCreateDBClick(object sender, System.EventArgs e)
{
   MySqlConnection conDatabase = new MySqlConnection("Data Source=localhost;" + "Persist Security Info=yes;" + "UserId=root; PWD=Whatever;");
   MySqlCommand cmdDatabase = new MySqlCommand("CREATE DATABASE BCR1;", conDatabase);
   conDatabase.Open();
   cmdDatabase.ExecuteNonQuery();
   conDatabase.Close();
}


这篇关于C#MySQL连接状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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