使用脚本启用与 sql express 的远程连接

发布于 2025-01-02 12:54:03 字数 2190 浏览 1 评论 0原文

我正在使用 sql server express 2008 部署应用程序。在我的应用程序的先决条件部分中,我已包含:

在此处输入图像描述

因此,当用户安装我的应用程序时,它也会安装 sql express。

然后我将能够连接到该数据库引擎,如下所示:

        try
        {
            // database should be in the same network
            SqlConnection conn =
                new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=True");
            conn.Open();
            MessageBox.Show("Connection succesfull");
        }
        catch
        {
            MessageBox.Show("Unable to connect");
        }

现在,当我安装不同的应用程序(客户端版本)时,我希望能够连接到该数据库引擎。我设法通过执行以下操作来连接到它:

        try
        {

            SqlConnection conn =
                new SqlConnection(@"Data Source=192.168.0.120\sqlexpress,22559; USER=sa; PASSWORD=*********");
            conn.Open();
            MessageBox.Show("Connection succesfull");
        }
        catch
        {
            MessageBox.Show("Unable to connect");
        }

为了使该代码正常工作,我必须执行以下操作:

输入图像描述这里

在此处输入图像描述


所以我的问题是:

如何使用代码配置它?当我部署应用程序时,我希望我的应用程序像它一样安装 sql express,但我也想启用 tcp/IP 连接,启用一些端口,最后为帐户“SA”创建密码,因为我无法连接到如果 sa 帐户没有密码,则远程数据库。

或者也许我要求太多而我做错了事。也许我应该只为我计划部署的数据库而不是数据库引擎做这一切。任何更容易的事情。我很难部署这个,也许部署本地数据库和 wcf 服务会更容易,以便在本地数据库上远程创建 CRUD 操作。

EIDT

我发现这 3 个链接声称可以做类似的事情,但我仍然无法使其工作。

1) http://support.microsoft.com/kb/839980

2) http://social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/c7d3c3af-2b1e-4273-afe9-0669dcb7bd02/

3) http://www.sql-questions.com/microsoft/SQL-Server/34211977/can-not-connect-to-sql-2008-express-on-same-lan.aspx

I am deploying an application with sql server express 2008. In the prerequisites section of my application I have included:

enter image description here

As a result when a user installs my application it will install sql express as well.

Then I will be able to connect to that database engine as:

        try
        {
            // database should be in the same network
            SqlConnection conn =
                new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=True");
            conn.Open();
            MessageBox.Show("Connection succesfull");
        }
        catch
        {
            MessageBox.Show("Unable to connect");
        }

Now when I install a different application(client version) I will like to be able to connect to that database engine. I managed to connect to it by doing something like:

        try
        {

            SqlConnection conn =
                new SqlConnection(@"Data Source=192.168.0.120\sqlexpress,22559; USER=sa; PASSWORD=*********");
            conn.Open();
            MessageBox.Show("Connection succesfull");
        }
        catch
        {
            MessageBox.Show("Unable to connect");
        }

In order for that code to work I had to do the following:

enter image description here

enter image description here


So my question is:

How could I configure this with code? When I deploy my application I want my application to install sql express like it does but I also whant to enable tcp/IP connections, enable some ports and lastly create a password for the account "SA" because I am not able to connect to the database remotly if the sa account does not have a password.

Or maybe I am asking to much and I am doing the wrong thing. perhaps I should do all this just for the database that I am planing on deploying not the database engine. whatever is easier. I have had a hard time deploying this maybe it will be eassier to deoploy a local database along with a wcf service in order to create CRUD operations on the local database remotely.

EIDT

I found this 3 links that claim on doing something similar and I still cannot make it work.

1) http://support.microsoft.com/kb/839980

2) http://social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/c7d3c3af-2b1e-4273-afe9-0669dcb7bd02/

3) http://www.sql-questions.com/microsoft/SQL-Server/34211977/can-not-connect-to-sql-2008-express-on-same-lan.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳男子 2025-01-09 12:54:03

下载sql server express 2008(SQLEXPR32_x86_ENU.exe)并将其放在我的C盘根目录中。然后我使用以下参数安装它:

C:\SQLEXPR32_x86_ENU.exe /q /hideconsole /action=Install /features=SQL /instancename=SQLEXPRESS /enableranu=1 /sqlsvcaccount="NT Authority\Network Service" /AddCurrentUserAsSqlAdmin /skiprules= RebootRequiredCheck /TCPENABLED=1

我添加 /TCPENABLED=1 以便启用 TCP/IP

downloaded sql server express 2008 (SQLEXPR32_x86_ENU.exe) and place it in the root of my c drive. then I install it with the following parameters:

C:\SQLEXPR32_x86_ENU.exe /q /hideconsole /action=Install /features=SQL /instancename=SQLEXPRESS /enableranu=1 /sqlsvcaccount="NT Authority\Network Service" /AddCurrentUserAsSqlAdmin /skiprules=RebootRequiredCheck /TCPENABLED=1

I add /TCPENABLED=1 in order to enable TCP/IP

沦落红尘 2025-01-09 12:54:03

我建议您创建修改后的引导程序包来自定义安装 Sql Server 2005 Express。

作为替代方案,您还可以在安装程序中使用自定义操作来使用 SMO 更改目标服务器。

像这样的事情:

Server server = new Server( "ServerName\\InstanceName" ); 
server.ConnectionContext.Connect();
server.Settings.LoginMode = ServerLoginMode.Mixed;
server.Settings.Alter();

我们使用 SMO 对象创建用户登录并将用户关联到我们创建的应用程序数据库。如果数据库不可用,甚至运行 sql 脚本来创建数据库。

请参阅这些链接:
配置 SQL Express安装
配置 SQL Server(如果包含)作为要求

注意:在 App.config 文件中创建 SQL Server 连接字符串设置,而不是在代码中放入核心内容。这将帮助您自定义应用程序首先运行定制,例如数据库创建。

I suggest you to create modified bootstrapper package to install Sql Server 2005 Express with customzation.

As an alternative, you can also use a custom action in your installer to change the targeted server using SMO.

Something like this:

Server server = new Server( "ServerName\\InstanceName" ); 
server.ConnectionContext.Connect();
server.Settings.LoginMode = ServerLoginMode.Mixed;
server.Settings.Alter();

We use SMO object to create user login and associate user to our created application database.. even run sql script to create database if database is not available..

Refer these links:
Configuring SQL Express During Installation
Configuring SQL Server when included as a requirement

Note: Create your sql server connection string settings in App.config file rather than putting hardcore in code.. this will help you customize application first run customization e.g. database creation.

痴梦一场 2025-01-09 12:54:03

这些可能会有一些帮助,我已经将它放在我的待办事项列表中一段时间​​了,对于我必须设置我的应用程序以与 Sql Server 2008 Express 一起运行的计算机。它基本上是一种设置脚本的方法,SQL08exp 安装程序将根据您在脚本中设置的内容读取并自动执行大量设置。

http://digitalformula。 net/articles/how-to-perform-an-unattended-installation-of-sql-server-2008-express/

http://blogesh.wordpress.com/2008/ 09/23/silent-install-of-sql-server-2008/

These might be of some help, I've had it on my todo list for a while for the computers I have to setup for my app to run with Sql Server 2008 Express. It's basically a way to setup a script that the SQL08exp installer will read and automate a lot of the setup according to what you set in the script.

http://digitalformula.net/articles/how-to-perform-an-unattended-installation-of-sql-server-2008-express/

http://blogesh.wordpress.com/2008/09/23/silent-install-of-sql-server-2008/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文