通过.NET WinForm应用程序连接到SQL Server连接到、应用程序、WinForm、NET

由网友(收起你的虚伪、给老子滚)分享简介:我想提出一个简单的C#.net的winform应用程序与Form1中,将连接到SQL Server。i am making a simple c#.net winform application with a form1, which will connect to sql server.我想,与SQL Serve...

我想提出一个简单的C#.net的winform应用程序与Form1中,将连接到SQL Server。

i am making a simple c#.net winform application with a form1, which will connect to sql server.

我想,与SQL Server建立连接之前,应用程序将要求用户输入登录名和放大器;密码进行连接。

i want that before establishing a connection to sql server , the application asks the user to enter the login name & password to connect.

这个我应该怎么做:

取登录名和放大器;密码在两个文本框和放大器;他们传递给连接字符串 要么 我应该将它们传递给app.config文件和放大器;然后用字符串从Form1.cs的App.config文件?

take the login name & password in two text boxes & pass them the to the connection string or should i pass them to the app.config file & then use the string from app.config file in the form1.cs?

这将是确定的安全问题?如果不是,那么什么是执行这一任务的其他方法?

will this be ok with the security issues? if not, then what are the other ways of implementing this task?

推荐答案

我会做这样的:

使用一个 SqlConnectionStringBuilder 组件 定义之类的服务器名,从数据库名称等您的app.config 在该组件还具有两个属性的用户名和密码 - 填补这些从一个对话框,您提示用户输入这些信息 在该SqlConnectionStringBuilder然后给你正确的连接字符串用于连接到SQL服务器 use a SqlConnectionStringBuilder component define things like server name, database name etc. from your app.config that component also has two properties for user name and password - fill those from a dialog box where you prompt the user for this information that SqlConnectionStringBuilder then gives you the proper connection string to use for connecting to your SQL Server

更新:

我的建议是将存储这样的基本的连接字符串:

My suggestion would be to store the basic connection string like this:

<configuration>
  <connectionStrings>
     <add name="MyConnStr" 
          connectionString="server=A9;database=MyDB;" />
  </connectionStrings>
</configuration>

然后装入这个骨架连接字符串 - 到你的 SqlConnectionStringBuilder (这是不完整的,仅是行不通的!):

Then load this "skeleton" connection string (which is incomplete - that alone won't work!) into your SqlConnectionStringBuilder:

string myConnStr = ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString;

SqlConnectionStringBuilder sqlcsb = new SqlConnectionStringBuilder(myConnStr);

然后抓住从用户的用户名和密码的对话框,然后将那些连接字符串生成器:

Then grab the user name and password from the user in a dialog box and add those to the connection string builder:

sqlcsb.UserID = tbxUserName.Text.Trim();
sqlcsb.Password = tbxPassword.Text.Trim();

,然后得到所产生的,完整的连接字符串中的 SqlConnectionStringBuilder

string completeConnStr = sqlcsb.ConnectionString;

using(SqlConnection _con = new SqlConnection(completeConnStr))
{
   // do whatever you need to do here....
}
阅读全文

相关推荐

最新文章