关于在不用Newtonsoft.Json转换的情况下快速给SqlCommand上参数的问题(Model转参数)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013542549/article/details/73695372

在日常工作中,我们在不使用Orm框架的情况下,需要手写DAL。在这个过程中,会发生很多重复性劳作,比如说在参数化查询中快速把一个Model对象转换成SqlCommand参数


下面提供一个比较简单快速给SqlCommand上参数的方法,下面看代码:

///_obj 参数为Model
public static SqlCommand CommToModelPara(object _obj)
{
    try
    {
        SqlCommand command = new SqlCommand();		//创建一个SqlCommand对象
        foreach (PropertyInfo info in _obj.GetType().GetProperties()) //遍历model
        {
            command.Parameters.Add(new SqlParameter(string.Format("@{0}", info.Name), info.GetValue(_obj)));//给SqlCommand对象上参数
        }
        return command;
    }
    catch
    {
        return new SqlCommand();//如果在出现异常的情况下,我们返回一个空的SqlCommand对象
    }
}


阅读更多
换一批

关于SqlCommand的第一个参数问题.

06-09

在微软发布的论坛代码中看到了如下语句:rnSqlCommand myCommand = new SqlCommand("Statistics_GetMostActiveModerators", myConnection);rnrn查了一下msdn,上面举的例子.第一个参数都是传入一些sql查询语句,如:rnstring mySelectQuery = "SELECT OrderID, Customer FROM Orders";rnSqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);rnrn那第一个情况写入"Statistics_GetMostActiveModerators",这样的情况,怎样查询到东西呢?rnrn请高人指点一下.rnrnrnrn附录1:微软发布的论坛的某个函数完整代码rn-------------------------------------------------------------------- rn/****************************************************************rn // GetMostActiveModeratorsrn //rn /// rn /// Returns a collection of the most active moderators.rn /// rn //rn ****************************************************************/rn public ModeratorCollection GetMostActiveModerators() rnrn // Create Instance of Connection and Command Objectrn SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString);rn SqlCommand myCommand = new SqlCommand("Statistics_GetMostActiveModerators", myConnection);rn SqlDataReader reader;rn ModeratorCollection moderators = new ModeratorCollection();rn Moderator moderator;rnrn // Mark the Command as a SPROCrn myCommand.CommandType = CommandType.StoredProcedure;rnrn // Execute the commandrn myConnection.Open();rn reader = myCommand.ExecuteReader();rnrn while (reader.Read()) rn moderator = new Moderator();rn moderator.Username = (string) reader["Username"];rn moderator.TotalPostsModerated = Convert.ToInt32(reader["PostsModerated"]);rnrn moderators.Add(moderator);rn rnrn reader.Close();rn myConnection.Close();rnrn return moderators;rnrn rn-------------------------------------------------------------------- rnrn附录2:msdn的例子rn-------------------------------------------------------------------- rn[C#] rnpublic void ReadMyData(string myConnString) rnrn string mySelectQuery = "SELECT OrderID, Customer FROM Orders";rn SqlConnection myConnection = new SqlConnection(myConnString);rn SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);rn myConnection.Open();rn SqlDataReader myReader = myCommand.ExecuteReader();rn try rn rn while (myReader.Read()) rn rn Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));rn rn rn finally rn rn // always call Close when done reading.rn myReader.Close();rn // always call Close when done reading.rn myConnection.Close();rn rn rn-------------------------------------------------------------------- rn

没有更多推荐了,返回首页