[英]What is the proper way to add subroutine to handle the connection string in C# WPF
I have a app.config and a connection string that works perfectly. 我有一个app.config和一个完美运行的连接字符串。
App.config App.config
<connectionStrings>
<add name="MyConnectionString"
connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=myDatabaseC;Data Source=agent_edx44-PC;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
My Form 我的表格
private void btnSave_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("uspINSERT",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
.....some code
}
private void bindDataGrid()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("uspSELECTALL",con);
cmd.Connection = con;
...some code
}
My problem is I want to put this line of code inside a subroutine so that I can call it in any methods. 我的问题是我想将此代码行放在子例程中,以便可以在任何方法中调用它。
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Here is what i have done so far: 到目前为止,这是我所做的:
private void MyConnection()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
MyConnection()
con.Open();
SqlCommand cmd = new SqlCommand("uspINSERT",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
.....some code
}
But it has an error saying that: 但是有一个错误说:
The name 'con' does not exist in the current context
名称“ con”在当前上下文中不存在
Here's how you should write those first two methods: 这是您应该如何编写前两个方法的方法:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
con.Open();
using (SqlCommand cmd = new SqlCommand("uspINSERT", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
//.....some code
}
}
}
private void bindDataGrid()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
con.Open();
using (SqlCommand cmd = new SqlCommand("uspSELECTALL", con))
{
cmd.Connection = con;
//...some code
}
}
}
Now,to get your new code to work you need to return the connection from the MyConnection
method: 现在,
MyConnection
新代码正常工作,您需要从MyConnection
方法返回连接:
private SqlConnection MyConnection()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
return con;
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection con = MyConnection())
{
con.Open();
using (SqlCommand cmd = new SqlCommand("uspINSERT", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
//.....some code
}
}
}
Now, I would go one step further and do this: 现在,我将进一步执行以下操作:
private void RunSqlCommand(string cmdText, Action<SqlConnection, SqlCommand> execute)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
con.Open();
using (SqlCommand cmd = new SqlCommand(cmdText, con))
{
cmd.Connection = con;
execute(con, cmd);
}
}
}
Now your btnSave_Click
method can become this: 现在,您的
btnSave_Click
方法可以变为:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
RunSqlCommand("uspINSERT", (con, cmd) =>
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
//.....some code
});
}
Change your MyConnection method to this, it returns a SqlConnection: 将您的MyConnection方法更改为此,它将返回SqlConnection:
private SqlConnection MyConnection()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
return con;
}
This is how to use: 这是使用方法:
using (var con = MyConnection())
{
//your code
}
using
block will dispose your DB connection after you have done with it. 用完block之后,
using
块将处置您的数据库连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.