当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - how to connect to localhost with this code?

问题描述:

i have a problem with this c# code. I need to connect it to mysql, localhost database, Please give me the correct code to [connetionString = "Data Source=ServerName;Initial Catalog=root;User ID=root;Password="; ] connect to the localhost.

using System;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

string connetionString = null;

SqlConnection cnn ;

**connetionString = "Data Source=ServerName;Initial Catalog=localhost;User ID=root;Password=";**

cnn = new SqlConnection(connetionString);

try

{

cnn.Open();

MessageBox.Show ("Connection Open ! ");

cnn.Close();

}

catch (Exception ex)

{

MessageBox.Show("Can not open connection ! ");

}

}

}

}

网友答案:

It should look a little more like this:

connetionString = "Data Source=localhost;Initial Catalog=<Name of the Database>;User ID=root;Password=";

The data source property is where you put the network location, the initial catalog is the name of the database (in mysql).

Edit:

However, I believe you'll need the mysql libraries, which I notice you're not using at the beginning. Get them from here: http://dev.mysql.com/downloads/connector/net/

The Data.SqlClient namespace is typically how you'd connect to MSSQL.

网友答案:

it seems you have tagged MySql connection, so preferably you want to use the mysql connection. Which you can download / install here: http://dev.mysql.com/downloads/connector/net/

Also it is wise to use the try-catch-finally approach. So that when the connection opens, and some exception occurs, the connection will always close afterwards.

As another addition, you could put the connectionstring in an App.Config or Web.Config so that you have the connectionstring available in all your files, and only have to adjust it in one place.

hope this will help you

using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient; //using the mysql dll

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=localhost;Initial Catalog=myDb;User ID=MyUser;Password=MyPass";
            MySqlConnection cnn = new MySqlConnection(connectionString);
            try
            {
                cnn.Open();
                MessageBox.Show("Connection Open ! ");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
                MessageBox.Show(ex.Message); //shows what error actually occurs
            }
            finally
            {
                cnn.Close();
            }
        }
    }
}
网友答案:

You are using System.Data.SqlClient in your connection which I think used for SQL Server. Your connection string is also not for MySQL Database. Try this one.

using System.Data.Odbc;


string connectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost;  
DATABASE=dbname; UID=myuserid; PASSWORD=mypassword;OPTION=3; POOLING=false;";

OdbcConnection DBCon = new OdbcConnection(connectionString);
if (DBCon.State == ConnectionState.Open)
{
    DBCon.Close();
}
DBCon.Open();
MessageBox.Show ("Connection Open ! ");
DBCon.Close();

Change the ODBC Driver version depending on what you are using.

Change the DATABASE, UID and PASSWORD value.

分享给朋友:
您可能感兴趣的文章:
随机阅读: