📄 mydatabase.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
namespace System.jdTools
{
public class myDataBase
{
private string db_Host = "";
private string db_UserID = "";
private string db_Pwd = "";
private string db_Name = "";
public string Host
{
get { return db_Host; }
set { db_Host = value; }
}
public string UserID
{
get { return db_UserID; }
set { db_UserID = value; }
}
public string Pwd
{
get { return db_Pwd; }
set { db_Pwd = value; }
}
public string Name
{
get { return db_Name; }
set { db_Name = value; }
}
public myDataBase(string Host, string UserID, string Pwd, string Name)
{
this.Host = Host;
this.UserID = UserID;
this.Pwd = Pwd;
this.Name = Name;
}
private string ConnectionString
{
get
{
return string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", Host, Name, UserID, Pwd);
}
}
public static SqlParameter CreateParameter(string ParamName, SqlDbType DbType, int Size, object Value)
{
SqlParameter parame=null;
if (Size > 0)
{
parame = new SqlParameter(ParamName, DbType, Size);
parame.Value = Value;
}
return parame;
}
public SqlDataReader GetDataReader(string SqlStr, params SqlParameter[] parames)
{
SqlConnection conn = new SqlConnection(ConnectionString);
if (conn.State == ConnectionState.Closed) conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
if(parames!=null)
{
foreach( SqlParameter parame in parames)
{
comm.Parameters.Add(parame);
}
}
SqlDataReader dr;
try
{
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
//if (comm.Connection.State ==ConnectionState.Open) comm.Connection.Close();
}
return dr;
}
public SqlDataReader GetDataReader(string SqlStr)
{
return GetDataReader(SqlStr,null);
}
public DataSet GetDataSet(string SqlStr, params SqlParameter[] parames)
{
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand comm = new SqlCommand(SqlStr, conn);
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataSet ds=new DataSet();
try
{
adapter.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (comm.Connection.State == ConnectionState.Open) comm.Connection.Close();
}
return ds;
}
public DataSet GetDataSet(string SqlStr)
{
return GetDataSet(SqlStr, null);
}
public int RunSql(string SqlStr, params SqlParameter[] parames)
{
SqlConnection conn = new SqlConnection(ConnectionString);
if (conn.State == ConnectionState.Closed) conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
if (parames != null)
{
foreach (SqlParameter parame in parames)
{
comm.Parameters.Add(parame);
}
}
int result = 0;
try
{
result= comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (comm.Connection.State == ConnectionState.Open) comm.Connection.Close();
}
return result;
}
public int RunSql(string SqlStr)
{
return RunSql(SqlStr, null);
}
public object RunSqlScalar(string SqlStr,params SqlParameter[] parames)
{
SqlConnection conn = new SqlConnection(ConnectionString);
if (conn.State == ConnectionState.Closed) conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
if (parames != null)
{
foreach (SqlParameter parame in parames)
{
comm.Parameters.Add(parame);
}
}
object result;
try
{
result = comm.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (conn.State == ConnectionState.Open) conn.Close();
}
return result;
}
public object RunSqlScalar(string SqlStr)
{
return RunSqlScalar(SqlStr,null);
}
public string GetFirstStr(string SqlStr, params SqlParameter[] parames)
{
object result = RunSqlScalar(SqlStr, parames);
if (result != null)
return result.ToString();
else
return "";
}
public string GetFirstStr(string SqlStr)
{
return GetFirstStr(SqlStr, null);
}
public int GetFirstInt(string SqlStr, params SqlParameter[] parames)
{
string result = GetFirstStr(SqlStr, parames);
return myCommon.ConvertToInt(result);
}
public int GetFirstInt(string SqlStr)
{
return GetFirstInt(SqlStr, null);
}
public bool GetFirstBool(string SqlStr, params SqlParameter[] parames)
{
string result = GetFirstStr(SqlStr, parames);
return myCommon.ConvertToBoolean(result);
}
public bool GetFirstBool(string SqlStr)
{
return GetFirstBool(SqlStr, null);
}
}
public class myCommon
{
/// <summary>
/// 字符串转换为整数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int ConvertToInt(string value)
{
///数据为空,返回-1
if (string.IsNullOrEmpty(value)) return 0;
int result = 0;
Int32.TryParse(value, out result);
return result;
}
/// <summary>
/// 字符串转换为时间
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static DateTime ConvertToDateTime(string value)
{
///定义初始化值
DateTime result = DateTime.Parse("1985-05-11");
if (string.IsNullOrEmpty(value)) return result;
DateTime.TryParse(value, out result);
return result;
}
/// <summary>
/// 字符串转换为实数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static decimal ConvertToDecimal(string value)
{
///定义初始值
decimal result = 0M;
if (string.IsNullOrEmpty(value)) return result;
decimal.TryParse(value, out result);
return result;
}
/// <summary>
/// 字符串转双精度浮点数字
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static double ConvertToDouble(string value)
{
double result = 0.0;
if (string.IsNullOrEmpty(value)) return result;
double.TryParse(value, out result);
return result;
}
/// <summary>
/// 字符串转换为布尔型
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool ConvertToBoolean(string value)
{
///定义初始值
bool result = false;
if (string.IsNullOrEmpty(value)) return result;
bool.TryParse(value, out result);
return result;
}
/// <summary>
/// 32位md5加密码算法
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public static string md5(string Str)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Str, "MD5").ToLower();
}
}
public class myConfig
{
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type">对象类型</param>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public static object configLoad(Type type, string filename)
{
FileStream fs = null;
try
{
// open the stream...
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (fs != null)
fs.Close();
}
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static void configSave(object obj, string filename)
{
FileStream fs = null;
// serialize it...
try
{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
}
/// <summary>
/// 文件操作
/// </summary>
public class myFile
{
public myFile()
{
}
public myFile(string FileName)
{
this.FileName = FileName;
}
private string filename = "";
public string FileName
{
get
{
return this.filename;
}
set
{
this.filename = value;
}
}
private string getPath()
{
if (FileName.Substring(0, 2) != "~/") FileName = "~/" + FileName;
return Application.StartupPath; //HttpContext.Current.Server.MapPath(FileName);
}
public void WriteFile(string Content)
{
FileStream fs = new FileStream(getPath(), FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.Write(Content);
sw.Dispose();
fs.Dispose();
}
public string ReadFile()
{
FileStream fs = new FileStream(getPath(), FileMode.OpenOrCreate, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
return sr.ReadToEnd();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -