简体   繁体   English

即使成功构建项目,应用程序也无法与MDF文件建立连接[Visual Studio 2010 C#]

[英]The application cannot make a connection with MDF file even building the project successfully [Visual Studio 2010 C#]

I just learned Visual Studio 2010 C# for 3 months. 我刚刚学习了Visual Studio 2010 C#3个月。 I added the data source call "PUBS.MDF" which is the sample database from Microsoft like "NORTHWIND.MDF". 我添加了数据源调用“PUBS.MDF”,它是Microsoft的示例数据库,如“NORTHWIND.MDF”。 and write the code to connect the data source with the following code : 并编写代码以使用以下代码连接数据源:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication18
{
 public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string constr = @"Data Source=.\SQLEXPRESS;
                        AttachDbFilename=|DataDirectory|\PUBS.mdf;
                        Integrated Security=True;User Instance=True;";
        SqlConnection connection = new SqlConnection(constr);
        connection.Open();
        string sql = "SELECT * FROM authors";
        SqlCommand command = new SqlCommand(sql, connection);
        textBox1.Text = (string)command.ExecuteScalar();
    }

  }
}

This code is debugged successfully as the picture below : 此代码已成功调试,如下图所示:

However, after I build the project, install in my computer (window7), and click to open the application (I also click "Run as administrator"), it show the error window as the picture below : 但是,在我构建项目后,在我的计算机上安装(window7),然后单击打开应用程序(我也单击“以管理员身份运行”),它会显示错误窗口,如下图所示:

Someone please help me. 有人请帮帮我。

PS. PS。

This is how I add the PUBS.MDF file in my application 这是我在我的应用程序中添加PUBS.MDF文件的方法

  1. Click tab "Data" 点击标签“数据”
  2. Click "Add New Data Souce..." 点击“添加新数据源...”
  3. Select "Database" 选择“数据库”
  4. Select "Dataset" 选择“数据集”
  5. Browse "PUBS.MDF" file 浏览“PUBS.MDF”文件

This is how I build the project 这就是我构建项目的方式

  1. Click tab "File" > Add > New Project > Visual Studio Installer > Setup Project 单击选项卡“文件”>“添加”>“新建项目”>“Visual Studio安装程序”>“安装项目”
  2. Right click "Application Folder" > "Add" > "Project Output" > "Primary Output" 右键单击“应用程序文件夹”>“添加”>“项目输出”>“主输出”
  3. Right click "Application Folder" > "Add" > "File..." > browse "PUBS.MDF" file 右键单击“应用程序文件夹”>“添加”>“文件...”>浏览“PUBS.MDF”文件
  4. At "Solution Explorer" window, right click the project's name > click "Build" and wait until it shows message "Build secceed" 在“解决方案资源管理器”窗口中,右键单击项目名称>单击“构建”,然后等待它显示“Build secceed”消息

I also upload the project, please download by this link --> http://www.4shared.com/zip/-HLqC-b_/WindowsFormsApplication18.html ? 我也上传了这个项目,请通过这个链接下载 - > http://www.4shared.com/zip/-HLqC-b_/WindowsFormsApplication18.html

I used to post the question almost the same with this --> Building project succeed with Access database, can search the data but cannot delete the record but in that case, the database was .accdb file and it can be solved by clicking "Run as an administrator" but this case was .MDF file and I don't know why just different type of database file but cannot solve by the same method. 我以前发布的问题几乎与此相同 - > 使用Access数据库构建项目成功,可以搜索数据但不能删除记录但在这种情况下,数据库是.accdb文件,可以通过单击“运行”来解决作为管理员“但这种情况是.MDF文件,我不知道为什么只是不同类型的数据库文件,但无法通过相同的方法解决。

change your connection string as below. 更改您的连接字符串如下。 it may work: 它可能有效:

      string constr =@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + System.Windows.Forms.Application.StartupPath + "\\PUBS.mdf;Integrated Security=True;User Instance=True";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM