我在Visual Studio 2012中开发了一个带有SQL Server 2012数据库的应用程序。当我尝试发布应用程序来测试它时,它在我的机器上正常工作(包括SQL Server数据文件夹中的数据库),但是当我将该已发布的应用程序移动到另一台机器时,它无法正常工作。
我想知道将项目与其数据库一起部署的最简单方法。 我已经看到将SQL数据库与我的应用程序集成的解决方案是使用localdb,但我不了解使用它的步骤。 我需要使用SQL Server 2012数据库部署应用程序的所有步骤,以便在另一台PC上安装应用程序,而无需在该PC上安装SQL Server 2012。
答案 0 :(得分:2)
您的应用程序无法在其他计算机上运行,因为您在没有数据库的本地计算机上使用相同的配置进行部署。
如果您没有在计算机上安装SQL Server,则可以使用SQL Server Express(默认情况下,它安装在Visual Studio中,除非您明确告诉它不要这样做)并更新{{ 1}}:
web.config
将数据库部署到服务器并更改连接字符串
<connectionStrings>
<add name="testCon"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
答案 1 :(得分:0)
不久前,我遇到了同样的问题。我查看了SQL Server,MySQL,SQL Server Express和SQL Server Compact版本。我想要一个独立应用程序的简单数据库。 SQL Server Compact适用于独立的自包含数据库。 SQLite是独立数据库的另一个绝佳选择,但这是另一个答案。 SQL Server Express的优缺点已在另一个答案中介绍。
要部署SQL Server Compact(CE),您可以包含SQLCE40Runtime_x86-ENU.exe
的安装程序,也可以手动包含安装程序为您创建的所需目录和Dll。有关更多部署信息,请参阅:https://msdn.microsoft.com/en-us/library/aa983326%28v=vs.140%29.aspx
我将使用的连接字符串是
ConnectionString = "Data Source=" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\DevEssai.sdf;Persist Security Info=False";
有关连接字符串的其他提示,请参阅:http://www.connectionstrings.com/
你最终做出的任何选择,你必须做出的每一个选择都有利弊。无论哪种方式,它都需要您进行一些研究,以便为您的应用选择最佳选择。不要被吓倒。一旦你进一步研究它,它就不像你最初想的那么难。这只是每个人都必须经历的学习曲线。
答案 2 :(得分:0)
我创建了一个简单的转换程序,将我的一个SQL Server表转换为SQL Server Compact Edition表。我刚创建了一个Windows窗体,其中只有一个按钮,表示“转换”。它将创建SQL Server CE数据库,然后从SQL Server数据库表中读取每条记录,并将其写入SQL Server Compact数据库中的等效表记录。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Data.SQLite;
namespace SampleConversion
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConvert_Click(object sender, EventArgs e)
{
string cmd = "";
int count = 0;
create_SQLCE_database(); // Create the SQL Server CE database file and a table within it
string SQLconnectionString = "server=(local); database=PTHData; Trusted_Connection=True;"; // open PTHData.mdf
string SQLCEconnectionString = "Data Source=" + Application.StartupPath + "\\pthData.sdf;Persist Security Info=False"; // open PTHDATA.sdf
// open the input and output database
SqlCeConnection SQLCEconnection = new SqlCeConnection(SQLCEconnectionString);
try
{
SQLCEconnection.Open();
}
catch (SqlCeException ex)
{
string errorMessages = "A SQL Server CE exception occurred on open.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
return;
}
SqlConnection SQLconnection = new SqlConnection(SQLconnectionString);
try
{
SQLconnection.Open();
}
catch (SqlException ex)
{
string errorMessages = "A SQL exception occurred on open.\n" + ex.Message;
MessageBox.Show( errorMessages, "Convert");
return;
}
//Databases are not open, time to convert
SqlCommand cmdread = new SqlCommand();
cmdread.Connection = SQLconnection;
cmdread.CommandText = "Select * from USTimeZones";
SqlDataReader drread = null;
SqlCeCommand cmdwrite = new SqlCeCommand();
cmdwrite.Connection = SQLCEconnection;
try
{
drread = cmdread.ExecuteReader();
while (drread.Read())
{
drread["timezone"].ToString();
cmd = "Insert into USTimeZones values ('" + drread["state"].ToString() + "','" +
drread["city"].ToString() + "','" + drread["county"].ToString() + "','" +
drread["timezone"].ToString() + "','" + drread["timetype"].ToString() + "','" +
drread["latitude"].ToString() + "','" + drread["longitude"].ToString() + "')";
cmdwrite.CommandText = cmd;
try
{
cmdwrite.ExecuteNonQuery();
count++;
}
catch (SqlCeException ex)
{
string errorMessages = "A SQL exception occurred on writing the SQL Server CE record.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
SQLCEconnection.Close();
SQLconnection.Close();
return;
}
}
}
catch (SqlException ex)
{
string errorMessages = "A SQL exception occurred reading records.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
}
catch (Exception ex)
{
string errorMessages = "A General exception occurred reading records.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
}
MessageBox.Show("Records written: " + count.ToString(), "Conversion complete");
drread.Close();
SQLconnection.Close();
SQLCEconnection.Close();
}
private void create_SQLCE_database()
{
string connectionString = "Data Source=" + Application.StartupPath + "\\pthData.sdf;Persist Security Info=False";
try
{
SqlCeEngine en = new SqlCeEngine(connectionString);
en.CreateDatabase();
}
catch (SqlCeException ex)
{
MessageBox.Show("Unable to create the SQL Server CE pthData database\n" + ex.Message, "Create SQL Server CE file/database error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
catch (Exception ex)
{
MessageBox.Show("Unable to create the SQL Server CE pthData database\n" + ex.Message, "Create SQL Server CE file/database error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
// file created, now create tables
SqlCeConnection cn = new SqlCeConnection(connectionString);
if (cn.State == ConnectionState.Closed)
cn.Open();
SqlCeCommand cmd;
string commandString = "Create table USTimeZones\n";
// create USTimeZones file
commandString = "Create table USTimeZones\r\n";
commandString += "(state nvarchar(30), city nvarchar(100), county nvarchar(50), timezone nvarchar(10), ";
commandString += "timetype int, latitude nvarchar(10), longitude nvarchar(10), ";
commandString += "PRIMARY KEY(state, city, county, timezone, timetype))";
cmd = new SqlCeCommand(commandString, cn);
try
{
cmd.ExecuteNonQuery();
}
catch (SqlCeException sqlexception)
{
MessageBox.Show(sqlexception.Message + "\n Command string: " + commandString, "Error creating USTimeZoness", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error creating USTimeZones", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
cn.Close();
}
private void btnSQLiteConvert_Click(object sender, EventArgs e)
{
string cmd = "";
int count = 0;
create_SQLite_database(); // Create the SQLite database file and a table within it
string SQLconnectionString = "server=(local); database=PTHData; Trusted_Connection=True;"; // open PTHData.mdf
string SQLiteconnectionString = "Data Source=" + Application.StartupPath + "\\pthData.sqlite;Version=3;";
// open the input and output database
SQLiteConnection SQLiteconnection = new SQLiteConnection(SQLiteconnectionString);
try
{
SQLiteconnection.Open();
}
catch (SQLiteException ex)
{
string errorMessages = "A SQLite exception occurred on open.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
return;
}
SqlConnection SQLconnection = new SqlConnection(SQLconnectionString);
try
{
SQLconnection.Open();
}
catch (SqlException ex)
{
string errorMessages = "A SQL exception occurred on open.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
return;
}
//Databases are not open, time to convert
SqlCommand cmdread = new SqlCommand();
cmdread.Connection = SQLconnection;
cmdread.CommandText = "Select * from USTimeZones";
SqlDataReader drread = null;
SQLiteCommand cmdwrite = new SQLiteCommand();
cmdwrite.Connection = SQLiteconnection;
try
{
drread = cmdread.ExecuteReader();
while (drread.Read())
{
drread["timezone"].ToString();
cmd = "Insert into USTimeZones values ('" + drread["state"].ToString() + "','" +
drread["city"].ToString() + "','" + drread["county"].ToString() + "','" +
drread["timezone"].ToString() + "','" + drread["timetype"].ToString() + "','" +
drread["latitude"].ToString() + "','" + drread["longitude"].ToString() + "')";
cmdwrite.CommandText = cmd;
try
{
cmdwrite.ExecuteNonQuery();
count++;
}
catch (SQLiteException ex)
{
string errorMessages = "An SQL exception occurred on writing the SQLite record.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
SQLiteconnection.Close();
SQLconnection.Close();
return;
}
}
}
catch (SqlException ex)
{
string errorMessages = "A SQL exception occurred reading records.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
}
catch (Exception ex)
{
string errorMessages = "A General exception occurred reading records.\n" + ex.Message;
MessageBox.Show(errorMessages, "Convert");
}
MessageBox.Show("Records written: " + count.ToString(), "Conversion complete");
drread.Close();
SQLconnection.Close();
SQLiteconnection.Close();
}
private void create_SQLite_database()
{
string connectionString = "Data Source=" + Application.StartupPath + "\\pthData.sqlite;Version=3;";
try
{
SQLiteConnection.CreateFile("pthData.sqlite");
}
catch (SQLiteException ex)
{
MessageBox.Show("Unable to create the SQLite database\n" + ex.Message + "\nConnection string: " + connectionString, "Create SQLite file/database error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
catch (Exception ex)
{
MessageBox.Show("Unable to create the SQLitedatabase\n" + ex.Message + "\nConnection string: " + connectionString, "Create SQLite file/database error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
// file created, now create tables
SQLiteConnection cn = new SQLiteConnection(connectionString);
if (cn.State == ConnectionState.Closed)
cn.Open();
SQLiteCommand cmd;
string commandString = "Create table if not exists USTimeZones\n";
// create time zones file
commandString += "(state nvarchar(30), city nvarchar(100), county nvarchar(50), timezone nvarchar(10), ";
commandString += "timetype int, latitude nvarchar(10), longitude nvarchar(10), ";
commandString += "PRIMARY KEY(state, city, county, timezone, timetype))";
cmd = new SQLiteCommand(commandString, cn);
try
{
cmd.ExecuteNonQuery();
}
catch (SQLiteException sqlexception)
{
MessageBox.Show(sqlexception.Message + "\n Command string: " + commandString, "Error creating USTimeZones", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n Command string: " + commandString, "Error creating USTimeZoness", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
}
}