当前位置: 首页>>代码示例>>C#>>正文


C# SqlParameter.Where方法代码示例

本文整理汇总了C#中System.Data.SqlClient.SqlParameter.Where方法的典型用法代码示例。如果您正苦于以下问题:C# SqlParameter.Where方法的具体用法?C# SqlParameter.Where怎么用?C# SqlParameter.Where使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.SqlClient.SqlParameter的用法示例。


在下文中一共展示了SqlParameter.Where方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InsertQuery

        public bool InsertQuery(SqlConnection cn, string tableName, SqlParameter[] sqlParams)
        {
            try
            {
                string query = "Insert into {0} ({1}) values({2})";
                string columns = string.Empty;
                string values = string.Empty;

                var availableFields = sqlParams.Where(x => x.Value != null).ToList();
                columns = String.Join(",", availableFields.Select(x => x.ParameterName));
                values = String.Join(",", availableFields.Select(x => String.Concat("@", x.ParameterName)));

                query = String.Format(query, tableName, columns, values);

                MakeSureConnectionIsOpen(cn);
                SqlCommand cmd = new SqlCommand(query, cn);

                if (availableFields != null)
                {
                    var idp = availableFields.Where(x => x.ParameterName.ToLower() == "id").FirstOrDefault();

                    if (idp != null)
                        availableFields.Remove(idp);

                    cmd.Parameters.AddRange(availableFields.ToArray());
                }
                cmd.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
开发者ID:nicholpereira,项目名称:EmployeeManagement156130,代码行数:35,代码来源:BaseSqlHandler.cs

示例2: UpdateQuery

        public bool UpdateQuery(SqlConnection cn, string tablename, SqlParameter[] sqlParams = null, string primaryColumnName = "Id")
        {
            try
            {

                string query = "Update {0} set {1} where {2}";

                var availableFields = sqlParams.Where(x => x.Value != null).ToList();
                List<string> cols = new List<string>();
                string whereClause = string.Empty;
                foreach (var item in availableFields)
                {
                    var s = String.Concat(item.ParameterName, "=", "@" + item.ParameterName);

                    if (item.ParameterName.ToLower() == primaryColumnName.ToLower())
                    {
                        whereClause = s;
                        continue;
                    }

                    cols.Add(s);
                }

                string columns = String.Join(",", cols);

                query = String.Format(query, tablename, columns, whereClause);

                MakeSureConnectionIsOpen(cn);

                SqlCommand cmd = new SqlCommand(query, cn);

                if (sqlParams != null)
                    cmd.Parameters.AddRange(availableFields.ToArray());

                cmd.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
开发者ID:nicholpereira,项目名称:EmployeeManagement156130,代码行数:43,代码来源:BaseSqlHandler.cs

示例3: AttachParameters

        // Since this class provides only static methods, make the default constructor private to prevent
        // instances from being created with "new SqlHelper()"
        /// <summary>
        ///     This method is used to attach array of SqlParameters to a SqlCommand.
        ///     This method will assign a value of DbNull to any parameter with a direction of
        ///     InputOutput and a value of null.
        ///     This behavior will prevent default values from being used, but
        ///     this will be the less common case than an intended pure output parameter (derived as InputOutput)
        ///     where the user provided no input value.
        /// </summary>
        /// <param name="command">The command to which the parameters will be added</param>
        /// <param name="commandParameters">An array of SqlParameters to be added to command</param>
        private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
        {
            if (command == null) throw new ArgumentNullException(nameof(command));
            if (commandParameters == null) return;

            foreach (var p in commandParameters.Where(p => p != null))
            {
                // Check for derived output value with no value assigned
                if ((p.Direction == ParameterDirection.InputOutput ||
                     p.Direction == ParameterDirection.Input) &&
                    (p.Value == null))
                {
                    p.Value = DBNull.Value;
                }
                command.Parameters.Add(p);
            }
        }
开发者ID:elyor0529,项目名称:MonetDB,代码行数:29,代码来源:SqlHelper.cs

示例4: AddSQLParameters

 public void AddSQLParameters(SqlCommand sqlcmd, SqlParameter[] sqlParam)
 {
     foreach (SqlParameter sqlParameter in sqlParam.Where(s => s != null))
         sqlcmd.Parameters.Add(sqlParameter);
 }
开发者ID:ProfilesRNS,项目名称:ProfilesRNS,代码行数:5,代码来源:DataIO.cs


注:本文中的System.Data.SqlClient.SqlParameter.Where方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。