C#如何打开基于MDF文件的数据库连接?打开连接时关闭

时间:2018-07-14 09:48:14

标签: c# sql-server tsql database-connection localdb

我想打开一个.mdf数据库连接,但是我遇到的问题是,当我用连接字符串打开连接时,我遇到一个错误,该错误提示我该连接未打开。

在这里您可以看到我的代码:

connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;Connect Timeout=30";

string secure_guid = "";

using (SqlConnection connection = new SqlConnection(connectionstring))
{
    connection.Open();

    SqlCommand command = connection.CreateCommand();
    command.CommandText = @"SELECT * FROM dbo.Clients
                            WHERE public_guid = @public_guid";

    command.Parameters.AddWithValue("public_guid", Public_Guid.ToString());

    secure_guid = Convert.ToString(command.ExecuteScalar());

    connection.Close();
}

0 个答案:

没有答案