wpf C# Window 应用程序中的连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18951692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connection string in C# Window application
提问by Ssasidhar
In window application, i am trying to work with database, how to write connection string in app.config. The below is connection string in app.config
在窗口应用程序中,我正在尝试使用数据库,如何在 app.config 中编写连接字符串。下面是 app.config 中的连接字符串
<configuration>
<appSettings >
<add key ="mycon"
value ="server=192*****;database=*****;
uid=**;pwd=*****;"/>
</appSettings>
</configuration>
Code to connect database:
连接数据库的代码:
SqlConnection con = new SqlConnection(
ConfigurationSettings.AppSettings["mycon"].ToString()); con.Open();
SqlCommand cmd = new SqlCommand("Usp_chat_login", con);
cmd.CommandType = CommandType.StoredProcedure ;
cmd.Parameters.Add("@userid", SqlDbType.VarChar, 20);
cmd.Parameters["@userid"].Value = textBox1.Text;
cmd.Parameters.Add("@password", SqlDbType.VarChar, 20);
cmd.Parameters["@password"].Value = textBox2.Text;
int reslt = cmd.ExecuteNonQuery(); con.Close();
if (reslt > 0)
{
MessageBox.Show("Yes");
}
else
{
MessageBox.Show("No");
}
Every time i am getting reslt=-1, even if i pass correct credentials
每次我收到时reslt=-1,即使我通过了正确的凭据
回答by Tim Medora
Every time i am getting reslt=-1, even if i pass correct credentials
每次我得到 reslt=-1,即使我通过了正确的凭据
This has nothing to do with the credentials, nor does it pertain to the config file. If authentication/authorization to the database failed, an exception would be thrown.
这与凭据无关,也与配置文件无关。如果对数据库的身份验证/授权失败,则会引发异常。
The problem is likely in your Usp_chat_loginprocedure.
问题很可能出在您的Usp_chat_login程序中。
See the documentationfor ExecuteNonQuery():
请参阅文档为ExecuteNonQuery():
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.
对于 UPDATE、INSERT 和 DELETE 语句,返回值是受命令影响的行数。当插入或更新的表上存在触发器时,返回值包括受插入或更新操作影响的行数以及受触发器影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,则返回值也是 -1。
Other notes:
其他注意事项:
Types which implement
IDisposableshould be disposed of, especially types which interact with unmanaged resources (e.g. database connections). A simple way to do this is to wrap the instances of these types in ausingstatement.Plain-text passwords are considered insecure/irresponsible in anyapplication.
IDisposable应该处理实现的类型,尤其是与非托管资源(例如数据库连接)交互的类型。一种简单的方法是将这些类型的实例包装在一个using语句中。在任何应用程序中,纯文本密码都被认为是不安全/不负责任的。
回答by Avinash Singh
Write it in App.XAML
在 App.XAML 中编写它
public partial class App : Application
{
public App()
{
string connectionStr = "Data Source=system\SQLExpress;Initial Catalog=DBName; user id=sa; password=test123;";
Application.Current.Properties["conStr"] = connectionStr;
}
}
and Access it in main form
并以主要形式访问它
static string strCon = Application.Current.Properties["conStr"].ToString();

