通过SQL连接到localhost [英] SQL connection to localhost

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

问题描述

我安装了DENWER,并在内置的PhpMyAdmin的帮助下在localhost上创建了一个数据库.当我在C#应用程序中连接到数据库时,出现未处理SQL异常".不明白我的错误在哪里...

I install DENWER and with help of built-in PhpMyAdmin create a database on localhost. When i connect to database in my C# application i get "SQL Exception was unhandled". Dont understand where is my mistake...

代码:

SqlConnection connection = new SqlConnection("Data Source=localhost; User Id=root; Password=; Initial Catalog=MyDB");
connection.Open();

推荐答案

由于phpMyAdmin是MySQL管理工具,因此我认为您已经安装了MySQL.显然,在进行下一步之前,应检查是否已安装并正在运行数据库.您可以在phpMyAdmin或mysql命令行工具中连接到它吗?

Since phpMyAdmin is a MySQL administration tool, I assume you've actually installed MySQL. Obviously you should check that you have a database installed and running before you go any further. Can you connect to it in phpMyAdmin or with the mysql command line tools?

尝试安装 MySQL .NET连接器,添加对MySql的引用.数据组装,然后:

Try installing the MySQL .NET connector, add a reference to the MySql.Data assembly and then:

var connection = new MySqlConnection(
               "server=localhost;user id=root;password=secret;database=MyDB;");
connection.Open();

通常,如果可以的话,应该将连接,命令和数据读取器对象包装在using中,以使它们能够正确处理

In general you should wrap your connection, command and data reader objects in using if you can so that they'll get disposed properly

using(var connection = new MySqlConnection("..."))
{
    connection.Open();
    using(var command = connection.CreateCommand())
    {
        command.CommandText = "...";

    }

    connection.Close();
}

等或将其包裹在最后,然后清理最终的物体.

etc. or wrap in a try-finally and clean up the objects in the finally.

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

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