简体   繁体   English

如何引用SQL连接

[英]How to reference SQL connection

I'm just learning asp.net/C# and am building my first application. 我只是学习asp.net/C#,正在构建我的第一个应用程序。

In my application, I am rendering data from sql on almost every view. 在我的应用程序中,我几乎在每个视图上都从sql渲染数据。

My controllers are getting large, because every time I make a request, I'm using somthing like: 我的控制器越来越大,因为每次发出请求时,我都会使用类似的东西:

try
  {
     sqlConnection = new SqlConnection(dbConnectionString);
     SqlCommand command = new SqlCommand("sp_Test", sqlConnection);
     command.CommandType = CommandType.StoredProcedure;
     sqlConnection.Open();
     return command.ExecuteNonQuery();
     sqlConnection.Close();
  }
catch (SqlException ex)
  {
     Console.WriteLine("SQL Error" + ex.Message.ToString());
     return 0;
  }

Is there a way to turn the sql into a simple using block? 有没有一种方法可以将sql转换为简单的using块?

Maybe something like: 也许像这样:

using(myConnection){
           SqlCommand command = new SqlCommand("sp_Test", sqlConnection);
           command.CommandType = CommandType.StoredProcedure;
}

There are many better approaches do it. 有很多更好的方法可以做到这一点。 You can create a SqlHelper class that can be used to execute stored procedures and SQL queries and also return DataReader and DataTable/DataSets . 您可以创建一个SqlHelper类,该类可用于执行存储过程和SQL查询,还可以返回DataReaderDataTable/DataSets

public class SqlHelper
{
   public SqlHelper(string connString)
   {
   }

   public DataSet GetDatasetByCommand(string Command);
   public SqlDataReader GetReaderBySQL(string strSQL);
   public SqlDataReader GetReaderByCmd(string Command);
   public SqlConnection GetSqlConnection();
   public void CloseConnection(); 
}

You can see one such sample here: 您可以在这里看到一个这样的示例:

http://www.nullskull.com/a/1295/sql-helper-class-in-c.aspx http://www.nullskull.com/a/1295/sql-helper-class-in-c.aspx

If you want more advanced approach you can go for Enterprise Library Data Access Block http://msdn.microsoft.com/en-us/magazine/cc163766.aspx 如果您需要更高级的方法,则可以使用企业库数据访问块http://msdn.microsoft.com/zh-cn/magazine/cc163766.aspx

The best thing to do is refactor that statement into a seperate method. 最好的办法是将该语句重构为单独的方法。 It looks like the only thing that could vary is the name of the procedure. 看起来唯一可以变化的是过程的名称。

So create an object with two properties, a boolean success and an error message. 因此,创建一个具有两个属性的对象,一个布尔成功和一个错误消息。

Call the function and pass in the name of the sql command. 调用该函数并传递sql命令的名称。 Your function should run your repeated code in the try block based on the given procedure name, then return an object with true/false and an error message if the call failed. 函数应基于给定的过程名称在try块中运行重复的代码,然后如果调用失败,则返回带有true / false和错误消息的对象。 This should make your controllers much smaller. 这将使您的控制器更小。 Example code for the controller: 控制器的示例代码:

var result = MyNewMethod("sp_Test");
if(!result.Success)
{
   Console.WriteLine(result.ErrorMessage);
   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM