C#中的SQL数据库连接 [英] Sql database connection in C#

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

问题描述

C#中的SQL数据库连接.net



我尝试过:



SQL Database connection in C#.net

What I have tried:

string str;
   SqlConnection cnn;
   str = @"server=(local);database=SMS_Data;Integrated Security=SSPI";
   SqlConnection cnn = new SqlConnection(connetionString);

推荐答案

嗯。看看你的代码:

Um. Look at your code:
string str;
SqlConnection cnn;
str = @"server=(local);database=SMS_Data;Integrated Security=SSPI";
SqlConnection cnn = new SqlConnection(connetionString);

什么是 connetionString ?因为你把实际字符串放入的变量叫做 str ...



你不应该努力代码连接字符串:它意味着您必须更改应用程序以在开发和生产之间切换,如果没有别的,这是危险的。当您释放产品时,它还会导致许多不同的地方发生变化。请改用配置文件或类似文件。这就是我这样做的方式(但对你来说可能有点过头了):实例存储 - 在应用程序之间共享配置数据的简单方法 [ ^ ]

What is in connetionString? Because the variable you put your actual string into is called str ...

You shouldn't "hard code" connection strings: it means you app has to be changed to switch between dev and production if nothing else, and that's dangerous. It also leads to loads of different places to change when you do release the product. Use a config file or similar instead. This is how I do it (but it's probably overkill for you): Instance Storage - A Simple Way to Share Configuration Data among Applications[^]


您没有提供有关您的问题的足够信息。然而......



首先,我同意OriginalGriff,因为:

You didn't provide enough information about your issue. Nevertheless...

First of all, i agree with OriginalGriff, because:
引用:

在应用程序的代码中嵌入连接字符串会导致安全漏洞和维护问题可以使用Ildasm.exe (IL反汇编程序)工具查看编译为应用程序源代码的未加密连接字符串。此外,如果连接字符串发生更改,则必须重新编译应用程序。出于这些原因,我们建议将连接字符串存储在应用程序配置文件中。

Embedding connection strings in your application's code can lead to security vulnerabilities and maintenance problems. Unencrypted connection strings compiled into an application's source code can be viewed using the Ildasm.exe (IL Disassembler) tool. Moreover, if the connection string ever changes, your application must be recompiled. For these reasons, we recommend storing connection strings in an application configuration file.



请参阅:连接字符串和配置文件| Microsoft Docs [ ^ ]



详情请见:

连接字符串语法| Microsoft Docs [ ^ ]

ASP.NET Web应用程序的SQL Server连接字符串| Microsoft Docs [ ^ ]

SQL Server连接字符串 - ConnectionStrings.com [ ^ ]


See: Connection Strings and Configuration Files | Microsoft Docs[^]

For further details, please see:
Connection String Syntax | Microsoft Docs[^]
SQL Server Connection Strings for ASP.NET Web Applications | Microsoft Docs[^]
SQL Server connection strings - ConnectionStrings.com[^]


这篇关于C#中的SQL数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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