win表单中的连接字符串 [英] connection string in win forms
本文介绍了win表单中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在创建一个Windows应用程序。我想在其中使用sql数据库...为此我想将连接字符串放在一个文件中(就像web应用程序中的web.cinfig)。
但是不知道在哪里放置。任何人都可以建议我这样做的最佳方法.... ??
hello all,
i am creating a windows application. in which i want to use sql database...for this i want to place connection string in a file (as like web.cinfig in web application).
but dont know where to place. Can any one suggest me the best ways to do that....??
推荐答案
假设您已经有一个配置连接字符串值的配置文件,< br $>
Assuming that you already have a config file with the connection string value set,
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings ["MyConnectionString"].ToString();
DataTable dt2 = new DataTable();
SqlCommand cmd = new SqlCommand("dbo.SP_GetData", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt2);
现在dt2包含来自Db的数据。
您的配置文件应该是这样的
Now dt2 contains the data from the Db.
Your config file shouldbe like this
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=xxxx;Initial Catalog=xxxxx;Integrated Security=True;";/>
</connectionStrings>
</configuration>
App.config有类似web.config的支持,但对于winforms。
看在哪里 [ ^ ]
App.config has similar support like web.config, but for winforms.
Look here[^]
通常,你把它放在一个应用程序的标准配置文件。请阅读: http://msdn.microsoft.com/en-us/library/ms254494.aspx [ ^ ]。
-SA
Normally, you put it in a standard configuration file of the application. Please read this: http://msdn.microsoft.com/en-us/library/ms254494.aspx[^].
Good luck,
—SA
这篇关于win表单中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文