c# - 无法填充 DataSet 但 MySqlCommand 工作正常

标签 c# mysql sql-server dataset sqlexception

所以我无法填充数据集。我收到以下异常:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.

The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)

但使用相同的 ConnectionString,我可以使用 MySqlCommand 类执行查询。

这在这里有效:

var con = ConfigurationManager.ConnectionStrings["conString"];
_helper = new MySqlHelper(con.ConnectionString);
string query = "SELECT Bezeichnung FROM stationen";
var reader = _helper.ExecuteReader(query);
while(reader.Read())
{
    //do something
}

但是如果我使用数据集,我在尝试填充数据集时会收到错误

var con = ConfigurationManager.ConnectionStrings["conString"];
string query = "SELECT Bezeichnung FROM stationen";

using (SqlDataAdapter adapter = new SqlDataAdapter(query, con.ConnectionString))
{
    var dataset = new U2ZFDataSet();
    adapter.Fill(dataset, "stationen");
}

我做错了什么?为什么数据集无法正常工作?

此外,这是 MySqlHelpter 代码:

private MySqlConnection _connection;

public MySqlHelper(string connectionString)
{
    try
    {
        _connection = new MySqlConnection(connectionString);
        _connection.Open();
    }
    catch (Exception ex)
    {
        if (_connection != null)
        {
            _connection.Dispose();
            _connection = null;
        }

        throw new Exception(connectionString, ex);
    }
}

public MySqlDataReader ExecuteReader(string sqlString)
{
    try
    {
        if (_connection == null || _connection.State != System.Data.ConnectionState.Open)
            throw new Exception("Connection closed");

        using (MySqlCommand command = new MySqlCommand(sqlString, _connection))
        {
            return command.ExecuteReader();
        }
    }
    catch (Exception ex)
    {
        throw new Exception(sqlString, ex);
    }
}

最佳答案

您应该在使用SqlDataAdapter之前打开连接。在第一个示例中,您使用助手,它会为您执行此操作,但在第二个示例中,您不再使用助手。

var con = ConfigurationManager.ConnectionStrings["conString"];
string query = "SELECT Bezeichnung FROM stationen";

con.Open();

using (SqlDataAdapter adapter = new SqlDataAdapter(query, con.ConnectionString))
{
    var dataset = new U2ZFDataSet();
    adapter.Fill(dataset, "stationen");
}

con.Close() -- do not forget to close it as well

检查此示例 here .

如果将连接打开包装在 try-catch block 中,就像在辅助类中所做的那样,效果会更好。

关于c# - 无法填充 DataSet 但 MySqlCommand 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45215879/

相关文章:

SQL Server - 有选择地将字段插入临时表

sql - 每个替代方案都有 T-SQL?

c# - 在 ASP.NET 中查找给定周(月)后的日期

C# 反射 : Fastest Way to Update a Property Value?

c# - 如何将 DateTimeFormatInfo.CurrentInfo 和 NumberFormatInfo.CurrentInfo 设置为不变?

php - 尝试从 Dreamweaver 中的登录信息写入表,结果全部显示为 1

php - Android Studio - 发送数据到mySql数据库

mysql - LEFT JOIN 上的 MAX() 值

sql - T-SQL 递归;多重递归?

c# - 如何将 'System.IO.IsolatedStorage.IsolatedStorageFileStream' 转换为 ImageSource?