问题描述
如何在带有sql server数据库的windows应用程序的visual studio 2010中创建安装文件
设置可以安装那么数据库也可以连接
值可插入,然后数据可以插入
此编码可以使用但是错误检查此代码
创建安装程序类别编码
how can create setup file in visual studio 2010 for windows application with sql server database
SETUP CAN INSTALL THEN DATABASE CAN ALSO CONNECT
VALUE CAN INSERT THEN DATA CAN INSERT
THIS CODING CAN USED BUT ERROR GIVEN CHECK THIS CODE
CREATE INSTALLER CLASS CODING
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Security.AccessControl;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
namespace CreatingDB
{
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
public Installer1()
{
InitializeComponent();
}
public void RestoreDatabase(String databaseName, String filePath, String serverName, String userName, String password, String dataFilePath, String logFilePath)
{
// Create Restore instance
Restore sqlRestore = new Restore();
// Point to database
BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
// Connect to DB Server
ServerConnection connection;
if (userName == "") // for Windows Authentication
{
SqlConnection sqlCon = new SqlConnection(@"Data Source=" + serverName + @"; Integrated Security=True;");
connection = new ServerConnection(sqlCon);
}
else // for Server Authentication
connection = new ServerConnection(serverName, userName, password);
// Restoring
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
sqlRestore.Action = RestoreActionType.Database;
String dataFileLocation = dataFilePath + databaseName + ".mdf";
String logFileLocation = logFilePath + databaseName + "_Log.ldf";
db = sqlServer.Databases[databaseName];
RelocateFile rf = new RelocateFile(databaseName, dataFileLocation);
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation));
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation));
sqlRestore.ReplaceDatabase = true;
sqlRestore.PercentCompleteNotification = 10;
sqlRestore.PercentComplete += new PercentCompleteEventHandler(sqlRestore_PercentComplete);
try
{
sqlRestore.SqlRestore(sqlServer);
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
db = sqlServer.Databases[databaseName];
db.SetOnline();
sqlServer.Refresh();
}
public event EventHandler<percentcompleteeventargs> PercentComplete;
void sqlRestore_PercentComplete(object sender, PercentCompleteEventArgs e)
{
if (PercentComplete != null)
PercentComplete(sender, e);
}
public event EventHandler<servermessageeventargs> Complete;
void sqlRestore_Complete(object sender, ServerMessageEventArgs e)
{
if (Complete != null)
Complete(sender, e);
}
public override void Commit(System.Collections.IDictionary savedState)
{
// Required permission
try
{
DirectorySecurity dirSec = Directory.GetAccessControl(Context.Parameters["TargetDir"]);
FileSystemAccessRule fsar = new FileSystemAccessRule
(@"NT AUTHORITY\NETWORK SERVICE"
, FileSystemRights.FullControl
, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit
, PropagationFlags.None
, AccessControlType.Allow);
dirSec.AddAccessRule(fsar);
Directory.SetAccessControl(Context.Parameters["TargetDir"], dirSec);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// Parameters that comes from setup project (CreatingDBSetup)
RestoreDatabase(Context.Parameters["databaseName"].ToString(), Context.Parameters["filePath"].ToString(), Context.Parameters
["serverName"].ToString(), Context.Parameters["userName"].ToString(), Context.Parameters["password"].ToString(), Context.Parameters
["dataFilePath"].ToString(), Context.Parameters["logFilePath"].ToString());
base.Commit(savedState);
}
}
}
==================== ===
APP.COFING CODING
=======================
APP.COFING CODING
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="conn" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=RegDemo;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
这个错误可以GIVEN
========================
SQL错误无法打开数据库DATABASENAME请求通过登录。登录失败
登录失败的用户'OM-PC \Administrator'
THIS ERROR CAN GIVEN
========================
SQL ERROR CANNOT OPEN DATABASE "DATABASENAME" REQUESTED BY THE LOGIN. THE LOGIN FAILED
LOGIN FAILED FOR USER 'OM-PC\Administrator'
推荐答案
SQL错误无法打开登录所请求的数据库DATABASENAME
SQL ERROR CANNOT OPEN DATABASE "DATABASENAME" REQUESTED BY THE LOGIN
似乎问题在于连接字符串。确保它是正确的。
Seems like the problem is with the Connection String. Make sure it is correct.
这篇关于如何在带有sql server数据库的windows应用程序的visual studio 2010中创建安装文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!