🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 1、sql语句返回DataTable ``` /// <summary> /// 返回静态DataTable /// </summary> /// <param name="sql">sql语句</param> /// <returns>返回dataTable</returns> public static DataTable ExecuteDataTable(string sql) { return SqlHelper.ExecuteDataset(connString, CommandType.Text, sql).Tables[0]; } ``` ## 用法 ``` DataTable dt = SqlEasy.ExecuteDataTable("select * from sys_users"); ``` ## 2、sql语句参数化返回DataTable ``` /// <summary> /// sql参数化查询 /// </summary> /// <param name="sql">sql语句带如KeyId等参数</param> /// <param name="para">sql参数如new SqlParameter("@KeyId", "9"),</param> /// <returns>返回datatable</returns> public static DataTable ExecuteDataTable(string sql, params SqlParameter[] para) { return SqlHelper.ExecuteDataset(connString, CommandType.Text, sql, para).Tables[0]; } public static DataTable ExecuteDataTable(string connectionString, string sql, params SqlParameter[] para) { return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, sql, para).Tables[0]; } ``` ## 用法 ``` //sql参数化查询 SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@KeyId", "9"), new SqlParameter("@username", "admin"), }; dt = SqlEasy.ExecuteDataTable("select * from sys_users where keyid=@KeyId", paras); Response.Write(JSONhelper.DataTableToJSON(dt)); ``` ## 3、执行SQL语句返回影响的行数 ``` /// <summary> /// 执行Sql语句 /// </summary> /// <param name="sql">sql语句</param> /// <returns>返回影响的行数</returns> public static int ExecuteNonQuery(string sql) { return SqlHelper.ExecuteNonQuery(connString, CommandType.Text, sql); } ``` ## 用法 ``` int k = SqlEasy.ExecuteNonQuery("update sys_users set qq='1000999' "); Response.Write(k); ``` ## 4、参数化执行sql语句返回影响的行数 ``` /// <summary> /// 参数化执行SQL语句返回影响的函数 /// </summary> /// <param name="sql"></param> /// <param name="para"></param> /// <returns></returns> public static int ExecuteNonQuery(string sql, params SqlParameter[] para) { return SqlHelper.ExecuteNonQuery(connString, CommandType.Text, sql, para); } ``` ## 用法 ``` //sql参数化查询 SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@KeyId", "9"), new SqlParameter("@username", "admin"), new SqlParameter("@qq", "229229"), }; int k = SqlEasy.ExecuteNonQuery("update sys_users set qq=@qq", paras); Response.Write(k); ``` ## 5、自建数据库连接 参数化执行SQL语句返回影响的行数 ``` /// <summary> /// 自建数据库连接 参数化执行SQL语句返回影响的行数 /// </summary> /// <param name="connectionString">数据库连接</param> /// <param name="sql">sql语句</param> /// <param name="para">sql参数</param> /// <returns>返回int影响的行数</returns> public static int ExecuteNonQuery(string connectionString, string sql, params SqlParameter[] para) { return SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql, para); } ``` ## 用法 ``` SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@KeyId", "9"), new SqlParameter("@username", "admin"), new SqlParameter("@qq", "229229"), }; string connectionString = "server=.;uid=sa;pwd=sa12we34;database=No1";//自建的数据库连接 int k = SqlEasy.ExecuteNonQuery(connectionString,"update sys_users set qq=@qq", paras); Response.Write(k); ```