日期:2014-05-18  浏览次数:20453 次

DbHelperSQL里一个函数的用法?
这个函数如何传参?
C# code

/// <summary>
        /// 执行SQL语句,返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        throw e;
                    }
                }
            }
        }



我是这样调用的但是提示参数不匹配
C# code

IDataParameter[] p = new IDataParameter[3];
            p[0] = new SqlParameter("@a",SqlDbType.NVarChar,50);
            p[1] = new SqlParameter("@b", SqlDbType.Text);
            p[2] = new SqlParameter("@c",SqlDbType.Text);
            string sql = "insert into report (user_name,content,url)values(@a,@b,@c)";
            return DbHelperSQL.ExecuteSql(sql, p);


请问要怎么调用?

------解决方案--------------------
你光定义了参数名字、数据类型和长度,没给它赋值啊。。。参数值米有啊。
------解决方案--------------------

string a=string.empty;

 SqlParameter[] spt = {


new SqlParameter("@a",SqlDbType.NVarChar,50)
};

spt[0].Value =a