【问题标题】:Failed to attach an auto-named database无法附加自动命名的数据库
【发布时间】:2014-05-17 08:06:10
【问题描述】:

所以我有一个 C# 软件可以将数据保存到我的数据库中,但是每次我运行我的程序并尝试保存数据时,我都会收到此消息,请帮忙?

try
{
    SqlConnection cnn = new SqlConnection(@"Data  Source=.\SQLEXPRESS;
        AttachDbFilename=C:\Users\Hp\Documents\Visual Studio 2010\Projects\Bank_System\Bank_System\Bank_System.sdf;
        Integrated Security=True;User Instance=True");
    cnn.Open();
    SqlCommand cmd1 = 
       new SqlCommand("insert into user values('" + 
           textBox6.Text + "','" + textBox1.Text + "','" + textBox4.Text + "'," + 
           textBox3.Text + ",'" + textBox2.Text +  "','" + textBox5.Text + "')",
           cnn);
    SqlDataReader dr1 = cmd1.ExecuteReader();
    dr1.Close();
    MessageBox.Show(" Record inserted ", " information inserted");
    cnn.Close();
}
catch (SqlException ex)
{
    MessageBox.Show(ex.Message);
}

【问题讨论】:

  • 请出示您的代码,也许我错了,但我不认为此消息来自框架代码

标签: c# database connection sdf


【解决方案1】:

将您的数据库 Bank_System.sdf 复制到 \bin\debug\ 文件夹中并更改您的连接字符串,如下所示:

SqlConnection cnn = new SqlConnection("Data Source=" +@".\SQLEXPRESS;
            AttachDbFilename=Bank_System.sdf;
            Integrated Security=True;User Instance=True");

应该可以了,如果发生错误,请尝试从位于\bin\debug\文件夹中的yourapp.exe 执行您的应用

【讨论】:

  • " 不支持关键字:'数据源'。" 这个异常一直显示给我
  • 我已经修改了,再试一次
  • 它显示了同样的老错误兄弟,“无法附加自动命名的数据库”
  • 试试这个连接字符串:“Data Source=(local);Initial Catalog=Bank_System.sdf;Integrated Security=True;”
  • 可能是您的系统拒绝连接到数据库,尝试将您的项目复制到 D: 或其他系统分区。
【解决方案2】:

您正在处理一个 SDF 文件。此文件适用于 SQL Server Compact,而不适用于 SQL Server Express(或完整)。

在这种情况下,连接字符串应该是:

 @"Data Source=<fullpath_and file_to_your_sdf_file>;Persist Security Info=False;"

请注意,在 C# 中,您需要在包含反斜杠等特殊字符的字符串前面添加逐字字符

使用 Sql Server Compact 需要安装 Microsoft Downloads 所需的库并使用正确的类。因此,请移除 SqlConnection 和 SqlCommand 类,并使用 SqlCeConnection 和 SqlCeCommand(对于您应用中使用的其他数据客户端类等等)。

当然,SqlCeConnection 类可以理解这种不同的连接字符串语法并允许使用 SDF 文件

也就是说,请修改构建 sql 命令的代码。像您的代码那样使用字符串连接是一种安全的错误方法。从解析错误(字符串中的引号会破坏语法)到更严重的错误,如Sql Injections

这可能是一种使用参数化查询的方法......

try
{
    string cmdText = "insert into user values(@p1, @p2, @p3,@p4,@p5,@p6)";
    using(SqlCeConnection cnn = new SqlCeConnection(@"Data Source=C:\Users\Hp\Documents\Visual Studio 2010\Projects\Bank_System\Bank_System\Bank_System.sdf;Integrated Security=True"))
    using(SqlCeCommand cmd1 = new SqlCeCommand(cmdText, cnn))
    {
        cnn.Open();
        cmd.Parameters.AddWithValue("@p1", textBox6.Text);
        cmd.Parameters.AddWithValue("@p2", textBox1.Text);
        cmd.Parameters.AddWithValue("@p3", textBox4.Text);
        cmd.Parameters.AddWithValue("@p4", textBox3.Text);
        cmd.Parameters.AddWithValue("@p5", textBox2.Text);
        cmd.Parameters.AddWithValue("@p6", textBox5.Text);
        cmd1.ExecuteNonQuery();
        MessageBox.Show(" Record inserted ", " information inserted");
     }
}
catch (SqlException ex)
{
    MessageBox.Show(ex.Message);
}

【讨论】:

  • 我试过了,然后开始给我“错误定位您的服务器”
  • 您是否将类从 SqlConnection 更改为 SqlCeConnection?您是否检查了给连接字符串的路径是否正确以及 SDF 文件是否存在?
  • 为什么要改成SqlCeConnection?
  • Sql Server Compact 是一种不同的数据库。您需要使用特定的类来使用它。 Sql Server vs Sql Server Compact
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多