/// <summary> /// 利用给定的SQL语句绑定给定的DataGrid /// 并将其中一列进行UrlEncode以作为超链的参数 /// 使用该函数时,将DataGrid超链列的URL字段处填“KeyField”即可 /// </summary> /// <param name="strSql">给定的SQL语句</param> /// <param name="DG">给定的DataGrid</param> /// <param name="ColumnName">需要UrlEncode的列名</param> public static void BindDataGrid(string strSql, DataGrid DG, string ColumnName) { try { DataSet DS = new DataSet(); DS = CreateDataSet(strSql); DS.Tables[0].Columns.Add("KeyField"); for (int i = 0; i < DS.Tables[0].Rows.Count; i++) { DS.Tables[0].Rows[i]["KeyField"] = HttpUtility.UrlEncode(DS.Tables[0].Rows[i][ColumnName].ToString().Trim()); } DG.DataSource = DS.Tables[0].DefaultView; if (DG.CurrentPageIndex > DG.PageCount - 1) { if (DG.PageCount > 0) { DG.CurrentPageIndex = DG.PageCount - 1; } else { DG.CurrentPageIndex = 0; } } DG.DataBind(); } catch (Exception e) { CFun.JsAlerT(e.Message.ToString()); } finally { //Close(); //Dispose(); } }
/// <summary> /// 根据单值型strSQL获取object型单值 /// </summary> /// <param name="SQLString">单值型strSQL语句</param> /// <returns>查询结果(object)</returns> public static object GetObjectSingle(string SQLString) { using (SqlConnection connection = new SqlConnection(ConnString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return(null); } else { return(obj); } } catch (System.Data.SqlClient.SqlException e) { connection.Close(); CFun.JsAlerT(e.Message.ToString()); } } } return(null); }
/// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">多条SQL语句</param> /// <returns>成功与否</returns> public static bool ExecuteSqlTran(ArrayList SQLStringList) { using (SqlConnection conn = new SqlConnection(ConnString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { for (int n = 0; n < SQLStringList.Count; n++) { string strsql = SQLStringList[n].ToString(); if (strsql.Trim().Length > 1) { cmd.CommandText = strsql; cmd.ExecuteNonQuery(); } } tx.Commit(); } catch (System.Data.SqlClient.SqlException e) { tx.Rollback(); CFun.JsAlerT(e.Message.ToString()); return(false); } } return(true); }
/// <summary> /// 分页方式获取DataSet(通过存储过程P_GridPage) /// </summary> /// <param name="SQL">SQL 语句</param> /// <param name="Page">当前页号</param> /// <param name="RecsPerPage">每页记录数</param> /// <param name="ID">索引字段</param> /// <param name="Sort">排序字段</param> /// <returns>当前页的记录集合</returns> public static DataSet GetPageDataSetByProc(string SQL, int Page, int RecsPerPage, string ID, string Sort) { DataSet ds = new DataSet(); try { SqlConnection Connection = new SqlConnection(ConnString); SqlDataAdapter objCmd = new SqlDataAdapter("P_GridPage", Connection); objCmd.SelectCommand.CommandType = CommandType.StoredProcedure; SqlParameter pSQL = new SqlParameter("@SQL", SqlDbType.VarChar, 4000); pSQL.Value = SQL; objCmd.SelectCommand.Parameters.Add(pSQL); SqlParameter pPage = new SqlParameter("@Page", SqlDbType.Int); pPage.Value = Page; objCmd.SelectCommand.Parameters.Add(pPage); SqlParameter pRecsPerPage = new SqlParameter("@RecsPerPage", SqlDbType.Int); pRecsPerPage.Value = RecsPerPage; objCmd.SelectCommand.Parameters.Add(pRecsPerPage); SqlParameter pID = new SqlParameter("@ID", SqlDbType.VarChar, 255); pID.Value = ID; objCmd.SelectCommand.Parameters.Add(pID); SqlParameter pSort = new SqlParameter("@Sort", SqlDbType.VarChar, 255); pSort.Value = Sort; objCmd.SelectCommand.Parameters.Add(pSort); objCmd.Fill(ds); objCmd.Dispose(); } catch (Exception e) { CFun.JsAlerT(e.Message.ToString()); } return(ds); }
/// <summary> /// 文件下载(文件推送型) /// </summary> /// <param name="filename">文件名含相对路径</param> /// <param name="isFullPath">是否物理地址1是 0 不是</param> public static void FileDown(string filename, string isFullPath) { if (filename != "") { if (isFullPath == "0") { if (CFun.Left(filename, 1) == "/") { filename = filename.Replace("/", "\\"); } if (CFun.Left(filename, 1) == @"\") { filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + CFun.Right(filename, filename.Length - 1); } else { filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + filename; } } System.IO.FileInfo file = new System.IO.FileInfo(filename); if (file.Exists) { System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8)); System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream"; System.Web.HttpContext.Current.Response.Filter.Close(); System.Web.HttpContext.Current.Response.WriteFile(file.FullName); System.Web.HttpContext.Current.Response.End(); } else { CFun.JsAlerT("文件不存在!"); //System.Web.HttpContext.Current.Response.Redirect("/error.aspx?msg=文件不存在!"); } } }