在每台计算机上运行的c#.net中创建exe时设置的路径 [英] what path to set that on creating exe in c#.net that runs on every computer

查看:105
本文介绍了在每台计算机上运行的c#.net中创建exe时设置的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlConnection con = new SqlConnection(Data Source = abhijeet-PC\\SQLEXPRESS; Initial Catalog = inventory_test; Integrated Security = True);





i已将连接字符串设置为此



现在我正在创建此项目的exe但是这个exe无法正常工作

其他计算机显示连接数据库时出错。



如何将数据库集成到可在每台计算机上运行的exe
$ b $我正在使用sql server 2008 r2和visual studio 2010

解决方案

你不能写一个可以在任何PC上运行的连接字符串。

没有法律规定所有SQL Server实例必须在特定的PC上,或者具有特定的实例名称,更不用说登录必须是基于Windows用户而不是单独的用户名和密码组合。



您可以做的最好的事情是使用某种形式的配置文件,a从那里读取你的连接字符串。

究竟是什么样的配置文件,以及你如何访问它将取决于你和你的应用程序环境。


HI它是对硬编码连接字符串不好,你可以将它保存在配置文件或注册表中,连接字符串必须加密。

但要做得很简单,我会告诉你实现需求的简单方法。



建议 :当您在连接中输入 Integrated Security = True 时字符串您的代码将自然地在安装数据库的计算机上工作,因为 Integrated Security = True 意味着您使用Windows身份验证(当前用户在安装时默认添加到sqlserver用户。)



但是当你使用其他计算机时,你可以继续使用 Integrated Security = True ,但你必须将这些计算机上的每个Windows用户添加到你的sqlserver的实例。 安全性更好



这样可以简单地在sqlserver实例中创建超级管理员用户像这样使用其他计算机上的唯一凭据



 创建登录[ sa]  WITH  PASSWORD = N '  pwd',DEFAULT_DATABASE = [databasename] CHECK_EXPIRATION = OFF,CHECK_POLICY = ON 
GO

EXEC sys.sp_addsrvrolemember @ loginame = N ' sa' @ rolename = N ' sysadmin'
GO





或者您可以使用sqlmanagement与设计器一起执行此操作:转到实例安全节点和连接节点,所以你可以创建superadmin用户现在连接字符串应该是(它可以在网络中的任何PC上工作)。不要忘记在sqlserver实例上允许距离连接并在Windows防火墙例外添加sqlserve)



 SqlConnection con = new SqlConnection(  数据源= abhijeet-PC\\SQLEXPRESS;初始目录= inventory_test;用户ID = sa;密码= pwd ); 


SqlConnection con = new SqlConnection("Data Source=abhijeet-PC\\SQLEXPRESS;Initial Catalog=inventory_test;Integrated Security=True");


i have seted the connection string to this

now i am creating exe of this project but this exe not working properly on
other computer shows error on connecting database.

how to integrate the database to the exe that it can run on each computer
i am using sql server 2008 r2 and visual studio 2010

解决方案

You can't write a single connection string that will work on any PC.
There is no law that says that all SQL Server instances must be on a particular PC, or have a particular instance name, much less that the login must be Windows user based instead of individual username and password combinations.

The best you can do is to use a configuration file of some form, and read your connection string from that.
Exactly what kind of config file, and how you access it will depend on you and you application environment.


HI it's not good to hard code connection string you can save it in the configuration file or in the regsitery and the connection string must be encrypt.
but to do simple i will show you the easy way to achieve your need.

Advice : when you put Integrated Security=True in your connection string your code will work naturally on the computer where the database is install because Integrated Security=True mean that you use the Windows authentification which (the current user is add to sqlserver users by default at installation.)

but when you go on other computer you can continue to use Integrated Security=True, but you must add each Windows user on those computer to the instance of your sqlserver. it's better for security

so do to simple you can create a super admin user in your sqlserver instance and use the unique credentials on others computer like this

CREATE LOGIN [sa] WITH PASSWORD=N'pwd', DEFAULT_DATABASE=[databasename] CHECK_EXPIRATION=OFF, CHECK_POLICY=ON
GO

EXEC sys.sp_addsrvrolemember @loginame = N'sa', @rolename = N'sysadmin'
GO



Or you can use sqlmanagement to do this with the designer : go to the instance security node and in the connexion node so you can create the superadmin user

now the connection string should be (it can work on any pc in the network. don't forget to allow distance connection on the sqlserver instance and Add sqlserve in windows firewall exception)

SqlConnection con = new SqlConnection("Data Source=abhijeet-PC\\SQLEXPRESS;Initial Catalog=inventory_test;User Id=sa;Password=pwd");


这篇关于在每台计算机上运行的c#.net中创建exe时设置的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆