c# - 如何验证您是否已连接到 C# 中的 MySQL 数据库?

标签 c# mysql wpf

我在 C# 中添加了对 MySQL 服务器的引用。我以为我的代码是对的。我知道连接值适合测试。这是我的错误:

{“建立与 SQL Server 的连接时发生了与网络相关或特定于实例的错误。未找到服务器或无法访问服务器。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接.(提供商:命名管道提供商,错误:40 - 无法打开与 SQL Server 的连接)”

我的 WPF 文本框和按钮:

<TextBox x:Name="textError" HorizontalAlignment="Left" Height="35" Margin="125,39,0,0" TextWrapping="Wrap" Text="If Error this will show it" VerticalAlignment="Top" Width="240"/>
    <TextBox x:Name="textUser" HorizontalAlignment="Left" Height="23" Margin="125,118,0,0" TextWrapping="Wrap" Text="Username" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textPassword" HorizontalAlignment="Left" Height="23" Margin="125,168,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top" Width="120"/>

我的 C# 代码:

namespace namespace1
{
 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

    }
    private MySqlConnection connection;
    private string server;
    private string database;
    private string uid;
    private string password;

    private void button_Click(object sender, RoutedEventArgs e)
    {
        if (textUser.Text != "" & textPassword.Text != "")
        {
            server = "localhost";
            database = "test";
            uid = "username";
            password = "password";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            connection = new MySqlConnection(connectionString);
            string queryText = @"SELECT Count(*) FROM Table1 
                         WHERE username = @Username AND password = @Password";

            using (SqlConnection cn = new SqlConnection(connectionString)) 
            using (SqlCommand cmd = new SqlCommand(queryText, cn))
            {
                cn.Open();
                cmd.Parameters.AddWithValue("@Username", textUser.Text);
                cmd.Parameters.AddWithValue("@Password", textPassword.Text);
                int result = (int)cmd.ExecuteScalar();
                if (result > 0)
                    MessageBox.Show("Logged In!");
                else
                    MessageBox.Show("User Not Found!");
            }
            }
        } 
    }
}

最佳答案

访问标题为 Download Connector/Net 的 MySQL 下载页面并继续显而易见的事情。请注意寻找最新的下载,因为它会老化。

使用适当的 MySQL 类

using MySql.Data.MySqlClient;

string connString = @"server=localhost;userid=drew;password=OpenSesame;database=stackoverflow";

.

        long lRet = 0;
        using (MySqlConnection lconn = new MySqlConnection(connString))
        {
            lconn.Open();
            using (MySqlCommand cmd = new MySqlCommand())
            {
                cmd.Connection = lconn;
                cmd.CommandText = @"select count(*) as theCount from batchQMetricsToUpdate where status=1";
                lRet = (long)cmd.ExecuteScalar();
            }
        }
        return(lRet);

添加引用截图:

enter image description here

关于c# - 如何验证您是否已连接到 C# 中的 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38089702/

相关文章:

c# - Resharper 快速修复将此限定符放在属性前面,而不仅仅是私有(private)字段

php - 使用 Join 查找 3 个表之间的特定值

c# - 仅在特定 GridViewColumn 中右键单击时显示上下文菜单

c# - 如何在类库项目中配置 Entity Framework ?

c# - 如何从 Windows 应用程序 C# 发布 word 文件

java - 你会在 MySQL 中映射 Java/Hibernate 中的 BigDecimal 什么类型?

mysql - 更新来自 SELECT 的数据

c# - DataTrigger 绑定(bind)到属性

c# - 在内存中渲染 Livecharts 图表并另存为图像

c# - 如何使用 MEF(托管可扩展性框架)从目录加载 dll