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