标题:我做的关于数据库和字符操作的公共类,欢迎大家下载指正,以期更完善
只看楼主
yj3385
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2004-12-1
 问题点数:0 回复次数:4 
我做的关于数据库和字符操作的公共类,欢迎大家下载指正,以期更完善
我做的关于数据库和字符操作的公共类,欢迎大家下载指正,以期更完善

3kx3VkcO.rar (28.25 KB) 我做的关于数据库和字符操作的公共类,欢迎大家下载指正,以期更完善

搜索更多相关主题的帖子: 数据库 字符 欢迎 
2005-10-13 11:23
yj3385
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2004-12-1
得分:0 

我以为上传的附件就能直接链接上呢,那么只好把代码贴上来了,哈哈

数据库操作如下:

using System; using System.Data.SqlClient; using System.Data; using PublicClass;

namespace PublicClass { /// <summary> /// DataControl 的摘要说明。 /// </summary> public class DataControl { public DataControl(string connstr) { tempconnectstring = connstr; } private SqlConnection conn;

private string tempconnectstring;

public string connectstring { get { return tempconnectstring; } set { tempconnectstring = value; } } private string TempReturnString; private string TempCurrDate;

private void openconn() { conn = new SqlConnection(connectstring); conn.Open(); } //取得服务器当前日期 public string GetDate(string monthnum,string flag) { try { openconn(); SqlCommand comm; comm = new SqlCommand(); comm.Connection = conn; if (monthnum == null) {monthnum = "";} if (monthnum == "") {monthnum = "0";} comm.CommandText = "select currdate = convert(char(10),dateadd("+monthnum+","+flag+",getdate()),121) "; TempCurrDate = comm.ExecuteScalar().ToString(); conn.Close(); } catch (Exception ex) { conn.Close(); SetErrorInfo(ex.Message); MyException me = new MyException(TempReturnString); throw me; } return TempCurrDate; }

//提项目数据到dataset中 public DataSet getdatatodatatable(string sqlstr) { DataSet TempDataSet = new DataSet(); try { openconn(); SqlDataAdapter DataAdapter = new SqlDataAdapter(sqlstr,conn); DataAdapter.SelectCommand.CommandTimeout=3000; DataAdapter.Fill(TempDataSet); conn.Close(); } catch (Exception ex) { conn.Close(); //查询出错提示 SetErrorInfo(ex.Message);

MyException me = new MyException(TempReturnString); throw me; } return TempDataSet; }

//判断数据是否存在 public string CheckDataExists(string sqlstr) { try { openconn(); SqlCommand com = new SqlCommand(); com.Connection = conn; com.CommandText = sqlstr; TempReturnString = com.ExecuteScalar().ToString(); conn.Close(); } catch (Exception ex) { conn.Close(); //查询出错提示 SetErrorInfo(ex.Message); MyException me = new MyException(TempReturnString); throw me; } return TempReturnString; }

//保存数据,传过来是字符串,执行字符串 public void SaveData(string sqlstr) { try { openconn(); SqlCommand com = new SqlCommand(); com.Connection = conn; com.CommandText = sqlstr; com.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { conn.Close(); //查询出错提示 SetErrorInfo(ex.Message); MyException me = new MyException(TempReturnString); throw me; } }

private void SetErrorInfo(string errstr) { Public_Class pc = new Public_Class(); TempReturnString = pc.SetErrorInfo(errstr); } } } 字符操作如下:

using System;

namespace PublicClass { /// <summary> /// StrControl 的摘要说明。 /// </summary> public class StrControl { public StrControl() { // // TOD 在此处添加构造函数逻辑 // } private string TempReturnString;

//替换字符(主要是防止SQL注入) public string ReplaceString(string sourcestr) { TempReturnString = ""; try { TempReturnString = sourcestr; if (TempReturnString == null) {TempReturnString = "";} TempReturnString = TempReturnString.Trim(); if (TempReturnString != "") { TempReturnString = TempReturnString.Replace("'","''"); } } catch (Exception ex) { SetErrorInfo(ex.Message); MyException me = new MyException(TempReturnString); throw me; } return TempReturnString; } //截取字符,cutflag = true 取前面的,cutflag = false 取后面的 public string getsubstr(string sourcestr,string flagstr,bool cutflag) { TempReturnString = ""; try { string tempstr = sourcestr; int posstr = -1; if (tempstr == null) { tempstr = ""; } tempstr = tempstr.Trim(); int flagstrlen = flagstr.Length; posstr = tempstr.LastIndexOf(flagstr);

if (posstr > 0) { if (cutflag == false) { TempReturnString = tempstr.Substring(posstr+flagstrlen,tempstr.Length-posstr-flagstrlen); } else { TempReturnString = tempstr.Substring(0,posstr); } } else { TempReturnString = ""; } } catch (Exception ex) { SetErrorInfo(ex.Message); MyException me = new MyException(TempReturnString); throw me; } return TempReturnString; } public string FormatNumToString(string sourcenum,int dotnum) { TempReturnString = ""; try { if (dotnum == 0) { TempReturnString = String.Format("{0:N}",Double.Parse(sourcenum)); } else { TempReturnString = String.Format("{0:N"+dotnum.ToString()+"}",Double.Parse(sourcenum)); } } catch (Exception ex) { SetErrorInfo(ex.Message); MyException me = new MyException(TempReturnString); throw me; } return TempReturnString; } private void SetErrorInfo(string errstr) { Public_Class pc = new Public_Class(); TempReturnString = pc.SetErrorInfo(errstr); } } }

用的公共函数如下:

using System;

namespace PublicClass { /// <summary> /// Public_Class 的摘要说明。 /// </summary> public class Public_Class { public Public_Class() { // // TOD 在此处添加构造函数逻辑 // } public string SetErrorInfo(string errstr) { string tempstr = errstr; if (tempstr == null) {tempstr = "";}

if (tempstr.IndexOf("\r") > 0) { tempstr = tempstr.Substring(0,tempstr.IndexOf("\r")); } return tempstr; } } public class MyException : System.ApplicationException { public MyException(string errormessage) { tempmessage = errormessage; } private string tempmessage;

public override string Message { get { return tempmessage; } } } }


2005-10-13 11:33
hxfly
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:5807
专家分:108
注 册:2005-4-7
得分:0 
不错
等我仔细看看哦



还有就是OleDb不能直接这样定义
如果OleDb能直接这样定义就好了

2005-10-14 09:18
冰封谷主
Rank: 4
等 级:贵宾
威 望:10
帖 子:226
专家分:20
注 册:2005-1-7
得分:0 
好东西呀,,,

2005-10-14 10:04
ltn
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-9-3
得分:0 
好啊
2005-10-15 09:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-30050-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.075650 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved