?? dataoperate.cs
字號:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
namespace SMS.BaseClass
{
class DataOperate
{
DataCon datacon = new DataCon();//聲明DataCon類的一個對象,以調用其方法
#region 綁定ComboBox控件
/// <summary>
/// 對ComboBox控件進行數據綁定
/// </summary>
/// <param name="M_str_sqlstr">SQL語句</param>
/// <param name="M_str_table">表名</param>
/// <param name="M_str_tbMember">數據表中字段名</param>
/// <param name="cbox">ComboBox控件ID</param>
public void cboxBind(string M_str_sqlstr, string M_str_table, string M_str_tbMember, ComboBox cbox)
{
DataSet myds = datacon.getds(M_str_sqlstr, M_str_table);
cbox.DataSource = myds.Tables[M_str_table];
cbox.DisplayMember = M_str_tbMember;
}
#endregion
#region 繪制餅圖
/// <summary>
/// 根據貨物所占百分比畫餅圖
/// </summary>
/// <param name="objgraphics">Graphics類對象</param>
/// <param name="M_str_sqlstr">SQL語句</param>
/// <param name="M_str_table">表名</param>
/// <param name="M_str_Num">數據表中貨物數</param>
/// <param name="M_str_tbGName">數據表中貨物名稱</param>
/// <param name="M_str_title">餅圖標題</param>
public void drawPic(Graphics objgraphics,string M_str_sqlstr, string M_str_table, string M_str_Num, string M_str_tbGName, string M_str_title)
{
DataSet myds = datacon.getds(M_str_sqlstr, M_str_table);
float M_flt_total = 0.0f, M_flt_tmp;
int M_int_iloop;
for (M_int_iloop = 0; M_int_iloop < myds.Tables[0].Rows.Count; M_int_iloop++)
{
M_flt_tmp = Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]);
M_flt_total += M_flt_tmp;
}
Font fontlegend = new Font("verdana", 9), fonttitle = new Font("verdana", 10, FontStyle.Bold);//設置字體
int M_int_width = 275;//白色背景寬
const int Mc_int_bufferspace = 15;
int M_int_legendheight = fontlegend.Height * (myds.Tables[0].Rows.Count + 1) + Mc_int_bufferspace;
int M_int_titleheight = fonttitle.Height + Mc_int_bufferspace;
int M_int_height = M_int_width + M_int_legendheight + M_int_titleheight + Mc_int_bufferspace;//白色背景高
int M_int_pieheight = M_int_width;
Rectangle pierect = new Rectangle(0, M_int_titleheight, M_int_width, M_int_pieheight);
//加上各種隨機色
Bitmap objbitmap = new Bitmap(M_int_width, M_int_height);//創建一個bitmap實例
objgraphics = Graphics.FromImage(objbitmap);
ArrayList colors = new ArrayList();
Random rnd = new Random();
for (M_int_iloop = 0; M_int_iloop < myds.Tables[0].Rows.Count; M_int_iloop++)
colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, M_int_width, M_int_height);//畫一個白色背景
objgraphics.FillRectangle(new SolidBrush(Color.LightYellow), pierect);//畫一個亮黃色背景
//以下為畫餅圖(有幾行row畫幾個)
float M_flt_currentdegree = 0.0f;
for (M_int_iloop = 0; M_int_iloop < myds.Tables[0].Rows.Count; M_int_iloop++)
{
objgraphics.FillPie((SolidBrush)colors[M_int_iloop], pierect, M_flt_currentdegree,
Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]) / M_flt_total * 360);
M_flt_currentdegree += Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]) / M_flt_total * 360;
}
//以下為生成主標題
SolidBrush blackbrush = new SolidBrush(Color.Black);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
objgraphics.DrawString(M_str_title, fonttitle, blackbrush, new Rectangle(0, 0, M_int_width, M_int_titleheight), stringFormat);
objgraphics.DrawRectangle(new Pen(Color.Black, 2), 0, M_int_height - M_int_legendheight, M_int_width, M_int_legendheight);
for (M_int_iloop = 0; M_int_iloop < myds.Tables[0].Rows.Count; M_int_iloop++)
{
objgraphics.FillRectangle((SolidBrush)colors[M_int_iloop], 5, M_int_height - M_int_legendheight + fontlegend.Height * M_int_iloop + 5, 10, 10);
objgraphics.DrawString(((String)myds.Tables[0].Rows[M_int_iloop][M_str_tbGName]) + " —— "
+ Convert.ToString(Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]) * 100 / M_flt_total) + "%", fontlegend, blackbrush,
20, M_int_height - M_int_legendheight + fontlegend.Height * M_int_iloop + 1);
}
objgraphics.DrawString("總貨物數是:" + Convert.ToString(M_flt_total), fontlegend, blackbrush, 5, M_int_height - fontlegend.Height);
string P_str_imagePath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,
Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
P_str_imagePath += @"\Image\image\" + DateTime.Now.ToString("yyyyMMddhhmss") + ".jpg";
objbitmap.Save(P_str_imagePath, ImageFormat.Jpeg);
objgraphics.Dispose();
objbitmap.Dispose();
}
#endregion
#region 文件壓縮
/// <summary>
/// 文件壓縮
/// </summary>
/// <param name="M_str_DFile">壓縮前文件及路徑</param>
/// <param name="M_str_CFile">壓縮后文件及路徑</param>
public void compressFile(string M_str_DFile, string M_str_CFile)
{
if (!File.Exists(M_str_DFile)) throw new FileNotFoundException();
using (FileStream sourceStream = new FileStream(M_str_DFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
using (FileStream destinationStream = new FileStream(M_str_CFile, FileMode.OpenOrCreate, FileAccess.Write))
{
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
{
compressedStream.Write(buffer, 0, buffer.Length);
}
}
}
}
#endregion
#region 驗證文本框輸入為數字
/// <summary>
/// 驗證文本框輸入為數字
/// </summary>
/// <param name="M_str_num">輸入字符</param>
/// <returns>返回一個bool類型的值</returns>
public bool validateNum(string M_str_num)
{
return Regex.IsMatch(M_str_num, "^[0-9]*$");
}
#endregion
#region 驗證文本框輸入為電話號碼
/// <summary>
/// 驗證文本框輸入為電話號碼
/// </summary>
/// <param name="M_str_phone">輸入字符串</param>
/// <returns>返回一個bool類型的值</returns>
public bool validatePhone(string M_str_phone)
{
return Regex.IsMatch(M_str_phone, @"\d{3,4}-\d{7,8}");
}
#endregion
#region 驗證文本框輸入為傳真號碼
/// <summary>
/// 驗證文本框輸入為傳真號碼
/// </summary>
/// <param name="M_str_fax">輸入字符串</param>
/// <returns>返回一個bool類型的值</returns>
public bool validateFax(string M_str_fax)
{
return Regex.IsMatch(M_str_fax, @"86-\d{2,3}-\d{7,8}");
}
#endregion
#region 用戶登錄
/// <summary>
/// 用戶登錄
/// </summary>
/// <param name="P_str_UserName">用戶名</param>
/// <param name="P_str_UserPwd">用戶密碼</param>
/// <returns>返回一個int類型的值</returns>
public int UserLogin(string P_str_UserName, string P_str_UserPwd)
{
SqlConnection sqlcon = datacon.getcon();
SqlCommand sqlcom = new SqlCommand("proc_Login", sqlcon);
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.Add("@UserName", SqlDbType.VarChar, 20).Value = P_str_UserName;
sqlcom.Parameters.Add("@UserPwd", SqlDbType.VarChar, 20).Value = P_str_UserPwd;
SqlParameter returnValue = sqlcom.Parameters.Add("returnValue", SqlDbType.Int, 4);
returnValue.Direction = ParameterDirection.ReturnValue;
sqlcon.Open();
try
{
sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlcom.Dispose();
sqlcon.Close();
sqlcon.Dispose();
}
int P_int_returnValue = (int)returnValue.Value;
return P_int_returnValue;
}
#endregion
#region 貨物入庫
/// <summary>
/// 貨物入庫
/// </summary>
/// <param name="P_int_GoodsID">貨物編號</param>
/// <param name="P_str_GoodsName">貨物名稱</param>
/// <param name="P_str_PrName">供應商名稱</param>
/// <param name="P_str_StoreName">倉庫名稱</param>
/// <param name="P_str_GoodsSpec">貨物規格</param>
/// <param name="P_str_GoodsUnit">計量單位</param>
/// <param name="P_int_GoodsNum">進貨數量</param>
/// <param name="P_dml_GoodsPrice">貨物單價</param>
/// <param name="P_str_HPeople">經手人</param>
/// <param name="P_str_Remark">備注</param>
/// <returns>返回一個int類型的值</returns>
public int InsertGoods(int P_int_GoodsID,string P_str_GoodsName,string P_str_PrName,string P_str_StoreName,
string P_str_GoodsSpec,string P_str_GoodsUnit,int P_int_GoodsNum,decimal P_dml_GoodsPrice,string P_str_HPeople,string P_str_Remark)
{
SqlConnection sqlcon = datacon.getcon();
SqlCommand sqlcom = new SqlCommand("proc_insertInStore", sqlcon);
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.Add("@GoodsID", SqlDbType.BigInt).Value = P_int_GoodsID;
sqlcom.Parameters.Add("@GoodsName", SqlDbType.VarChar, 50).Value = P_str_GoodsName;
sqlcom.Parameters.Add("@PrName", SqlDbType.VarChar, 100).Value = P_str_PrName;
sqlcom.Parameters.Add("@StoreName", SqlDbType.VarChar, 100).Value = P_str_StoreName;
sqlcom.Parameters.Add("@GoodsSpec", SqlDbType.VarChar, 50).Value = P_str_GoodsSpec;
sqlcom.Parameters.Add("@GoodsUnit", SqlDbType.Char, 8).Value = P_str_GoodsUnit;
sqlcom.Parameters.Add("@GoodsNum", SqlDbType.BigInt).Value = P_int_GoodsNum;
sqlcom.Parameters.Add("@GoodsPrice", SqlDbType.Money).Value = P_dml_GoodsPrice;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -