简体   繁体   中英

How to attach a database to a setup

This is my First App with a Database.It installs and runs perfectly on my PC. But when installed on another computer,it throws an error:

System.Data.SqlClient.SqlException (0x80131904): 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: SQL Network Interfaces, error: 50 - Local Database 
Runtime error occurred. The specified LocalDB instance does not exist.

What I need is, the setup file must self contain a database locally so that I can store some data and retrieve it.

The sample app is here :

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace form1
{
   public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }
    SqlDataAdapter da;
    DataSet ds;
    SqlConnection con;
    private void button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\sql.mdf;Integrated Security=True");
        da = new SqlDataAdapter("insert into STUDENTDATA(STUDENT,CLASS,SEX)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", con);
        ds = new DataSet();
        da.Fill(ds);
        MessageBox.Show("Registration has been successful");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();

    }
  }
}

Note::The other computer has Visual Studio Installed in it.

Try this, it worked for me.

Go to Tools/Options/Database Tools/click Data connection/Now remove Sql Server Instance Name from Right hand box/click ok.

Then try to add another database: Open Solution Explorer/Right click on project/Add New Item/Select Service-based database/click Add

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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