本地sqlexpress数据库的连接字符串? [英] Connection string for local sqlexpress database?

查看:347
本文介绍了本地sqlexpress数据库的连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望通过代码连接我的sqlexpress2016数据库。

为此,我尝试构建一个连接字符串,但是它给出了错误..



任何指导都会有所帮助。





Hi,
I wish to connect my sqlexpress2016 database by code.
For that I try to build a connection string, but it's giving error ..

Any guidance's will be helpful.


static String MyDBConStr = "Data Source=.\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";





错误:



Error :

.\sqlexpress           class system.string Represnts text as a series of Unicode characters       Unrecognized escape sequence





感谢问候

PARAMAN



我尝试过:



构建数据库连接字符串



Thanks Regards
PARAMAN

What I have tried:

Build Database connection string

推荐答案

字符串中间是否有转义字符(\)... 2.4.4.5字符串文字(C#) [ ^ ]

您有两种选择:

1。将转义字符加倍。\\sqlexpress

2.使用@仅为此字符串禁用转义字符 @ ...
Do you have an escape character in the middle of the string (\)... 2.4.4.5 String literals (C#)[^]
You have two options:
1. Double the escape char .\\sqlexpress
2. Use @ to disable escape character for this string only @"..."


'\'是C#中的特殊字符 - 它表示以下字符是特殊代码,应该合并产生一个新角色。例如,\ n是换行符,\是双引号,\\是反斜杠。

试试这个:

'\' is a special character in C# - it indicates that the following character is a "special code" and should be combined to produce a new character. For example, "\n" is a newline, "\"" is a double quote, and "\\" is a backslash.
Try this:
static String MyDBConStr = "Data Source=.\\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";

或更好,前缀teh strign with'@'关闭反斜杠:

Or better, prefix teh strign with '@' to turn off backslashes:

static String MyDBConStr = @"Data Source=.\sqlexpress; Initial Catalog=AddressDb; Integrated Security=True";



但是你不应该这样做:连接字符串应该存储在配置文件中,因此你不必重新编译你的应用程序对于每个新版本或DB服务器中的更改!


But you shouldn't do that anyway: connection strings should be stored in a configuration file, so you don't have to recompile your application for each new release or change in the DB server!


这篇关于本地sqlexpress数据库的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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