簡體   English   中英

如何將數據庫附加到設置

[英]How to attach a database to a setup

這是我的第一個帶有數據庫的應用程序,它可以在我的PC上完美安裝並運行。 但是,當安裝在另一台計算機上時,它將引發錯誤:

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.

我需要的是,安裝文件必須自己在本地包含一個數據庫,以便我可以存儲一些數據並檢索它。

示例應用程序在這里:

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();

    }
  }
}

注意::另一台計算機中裝有Visual Studio。

試試這個,對我有用。

轉到工具/選項/數據庫工具/單擊數據連接/現在從右側框中刪除Sql Server實例名稱/單擊確定。

然后嘗試添加另一個數據庫:打開解決方案資源管理器/右鍵單擊項目/添加新項/選擇基於服務的數據庫/單擊添加

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM