关于数组传递参数! 重载一个public virtual bool ExecuteSql(string SqlText,CommandType commandType)C#不支持参数的默认值,VB.net可以 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 顺便把全部的代码帖出来.小弟初学,想写程序巩固语法.这是一个数据库操作类,功能不是很多,主要是执行一般的操作,没有Scale和XML相关的东西.大侠看看,写这样的类,我哪里写的不好,请指点!万分感谢!using System;using System.Data.SqlClient;using System.Data;namespace myHelper{ public class DataConnect { private string _ServerName; private string _DataBaseName; private string _LoginName; private string _LoginPwd; private string _ConnectionString; private SqlConnection myConn; private SqlCommand myCmd; private SqlDataAdapter myAdapter; #region 与连接相关的属性 public string ServerName { get { return _ServerName; } set { this._ServerName=value; } } public string DataBaseName { get { return this._DataBaseName; } set { this._DataBaseName=value; } } public string LoginName { get { return this._LoginName; } set { this._LoginName=value; } } public string LoginPwd { get { return this._LoginPwd; } set { this._LoginPwd=value; } } #endregion 与连接相关的属性 //默认构造函数 public DataConnect() { } //传递连接字符串各部分值 public DataConnect(string strServerName, string strDataBaseName, string strLoginName, string strLoginPwd) { this.ServerName=strServerName; this.DataBaseName=strDataBaseName; this.LoginName=strLoginName; this.LoginPwd=strLoginPwd; } //直接传递连接字符串 public DataConnect(string strConnection) { this._ConnectionString=strConnection; } //生成获取连接字符串 public string GetConnectionString() { if (_ConnectionString==null) { this._ConnectionString="Server="+this.ServerName+ ";Database="+this.DataBaseName+ ";UID="+this.LoginName+ ";PWD="+this.LoginPwd+";"; } return _ConnectionString; } //打开连接 public void Open() { if(this.myConn.State!=ConnectionState.Open) { try { myConn=new SqlConnection(); myConn.ConnectionString=this.GetConnectionString(); this.myConn.Open(); } catch(Exception ex) { throw new Exception(ex.Message); } } } //关闭连接 public void Close() { try { if(this.myConn.State!=ConnectionState.Closed) { try { this.myConn.Close(); } catch(Exception ex) { throw new Exception(ex.Message); } } } catch(Exception ex) { throw new Exception(ex.Message); } } //生成调用存储过程的Command private SqlCommand CreateCommand(SqlConnection myConn, SqlParameter[] para,string SqlText,CommandType commandType) { SqlCommand myCommand=new SqlCommand(SqlText,myConn); myCommand.Connection=myConn; myCommand.CommandType=commandType; myCommand.CommandText=SqlText; if(para!=null) { foreach(SqlParameter p in para) { myCommand.Parameters.Add(p); } } return myCommand; } //生成执行SQL查询的Command private SqlCommand CreateCommand(SqlConnection myConn,string SqlText,CommandType commandType) { SqlCommand myCommand=new SqlCommand(SqlText,myConn); myCommand.Connection=myConn; myCommand.CommandType=commandType; myCommand.CommandText=SqlText; return myCommand; } //调用Procedure返回DataSet public virtual bool ExecuteSP(string SqlText,SqlParameter[] para,CommandType commandType,DataSet myDataSet) { this.myCmd=this.CreateCommand(this.myConn,para,SqlText,commandType); this.myAdapter=new SqlDataAdapter(myCmd); try { this.myAdapter.Fill(myDataSet); return true; } catch(Exception ex) { return false; } } //调用Procedure public virtual bool ExecuteSP(string SqlText,SqlParameter[] para,CommandType commandType) { this.myCmd=this.CreateCommand(this.myConn,para,SqlText,commandType); try { myCmd.ExecuteNonQuery(); return true; } catch(Exception ex) { return false; } } //调用Procedure返回SqlDataReader public virtual bool ExecuteSP(string SqlText,SqlParameter[] para,CommandType commandType,System.Data.SqlClient.SqlDataReader myReader) { this.myCmd=this.CreateCommand(this.myConn,para,SqlText,commandType); try { myReader=myCmd.ExecuteReader(); return true; } catch(Exception ex) { return false; } } //执行Sql语句返回DataSet public virtual bool ExecuteSql(string SqlText,CommandType commandType,DataSet myDataSet) { this.myCmd=this.CreateCommand(this.myConn,SqlText,commandType); this.myAdapter=new SqlDataAdapter(myCmd); try { this.myAdapter.Fill(myDataSet); return true; } catch(Exception ex) { return false; } } //执行Sql语句 public virtual bool ExecuteSql(string SqlText,CommandType commandType) { this.myCmd=this.CreateCommand(this.myConn,SqlText,commandType); try { myCmd.ExecuteNonQuery(); return true; } catch(Exception ex) { return false; } } //执行Sql语句返回DataReader public virtual bool ExecuteSql(string SqlText,CommandType commandType,SqlDataReader myReader) { this.myCmd=this.CreateCommand(this.myConn,SqlText,commandType); try { myReader=myCmd.ExecuteReader(); return true; } catch(Exception ex) { return false; } } } } 关于sqldatareader的问题 C#如何做程序 取任意网站 的标题,传网址。。。。。 C# winform 导出excel文件的格式怎么设置? 关于Nhibernate建立领域模型,生成对应数据库表问题。 Seesion强制类型转换 新手求教,如何实现不规则Textbox文本框 XML 文件序列化问题------XSD winform下:在dataGrid中增加一boolean列,的问题?急待解决! 关于运行.exe文件问题 时间间隔TimeSpan有什么用? C#和VC++的性能比较如何? 关于方法返回值问题的请教!急!
大侠看看,写这样的类,我哪里写的不好,请指点!万分感谢!using System;
using System.Data.SqlClient;
using System.Data;
namespace myHelper
{
public class DataConnect
{
private string _ServerName;
private string _DataBaseName;
private string _LoginName;
private string _LoginPwd;
private string _ConnectionString;
private SqlConnection myConn;
private SqlCommand myCmd;
private SqlDataAdapter myAdapter;
#region 与连接相关的属性
public string ServerName
{
get
{
return _ServerName;
}
set
{
this._ServerName=value;
}
}
public string DataBaseName
{
get
{
return this._DataBaseName;
}
set
{
this._DataBaseName=value;
}
}
public string LoginName
{
get
{
return this._LoginName;
}
set
{
this._LoginName=value;
}
}
public string LoginPwd
{
get
{
return this._LoginPwd;
}
set
{
this._LoginPwd=value;
}
}
#endregion 与连接相关的属性
//默认构造函数
public DataConnect()
{
}
//传递连接字符串各部分值
public DataConnect(string strServerName,
string strDataBaseName,
string strLoginName,
string strLoginPwd)
{
this.ServerName=strServerName;
this.DataBaseName=strDataBaseName;
this.LoginName=strLoginName;
this.LoginPwd=strLoginPwd;
}
//直接传递连接字符串
public DataConnect(string strConnection)
{
this._ConnectionString=strConnection;
}
//生成获取连接字符串
public string GetConnectionString()
{
if (_ConnectionString==null)
{
this._ConnectionString="Server="+this.ServerName+
";Database="+this.DataBaseName+
";UID="+this.LoginName+
";PWD="+this.LoginPwd+";";
}
return _ConnectionString;
}
//打开连接
public void Open()
{
if(this.myConn.State!=ConnectionState.Open)
{
try
{
myConn=new SqlConnection();
myConn.ConnectionString=this.GetConnectionString();
this.myConn.Open();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
}
//关闭连接
public void Close()
{
try
{
if(this.myConn.State!=ConnectionState.Closed)
{
try
{
this.myConn.Close();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
//生成调用存储过程的Command
private SqlCommand CreateCommand(SqlConnection myConn, SqlParameter[] para,string SqlText,CommandType commandType)
{
SqlCommand myCommand=new SqlCommand(SqlText,myConn);
myCommand.Connection=myConn;
myCommand.CommandType=commandType;
myCommand.CommandText=SqlText;
if(para!=null)
{
foreach(SqlParameter p in para)
{
myCommand.Parameters.Add(p);
}
}
return myCommand;
}
//生成执行SQL查询的Command
private SqlCommand CreateCommand(SqlConnection myConn,string SqlText,CommandType commandType)
{
SqlCommand myCommand=new SqlCommand(SqlText,myConn);
myCommand.Connection=myConn;
myCommand.CommandType=commandType;
myCommand.CommandText=SqlText;
return myCommand;
}
//调用Procedure返回DataSet
public virtual bool ExecuteSP(string SqlText,SqlParameter[] para,CommandType commandType,DataSet myDataSet)
{
this.myCmd=this.CreateCommand(this.myConn,para,SqlText,commandType);
this.myAdapter=new SqlDataAdapter(myCmd);
try
{
this.myAdapter.Fill(myDataSet);
return true;
}
catch(Exception ex)
{
return false;
}
}
//调用Procedure
public virtual bool ExecuteSP(string SqlText,SqlParameter[] para,CommandType commandType)
{
this.myCmd=this.CreateCommand(this.myConn,para,SqlText,commandType);
try
{
myCmd.ExecuteNonQuery();
return true;
}
catch(Exception ex)
{
return false;
}
}
//调用Procedure返回SqlDataReader
public virtual bool ExecuteSP(string SqlText,SqlParameter[] para,CommandType commandType,System.Data.SqlClient.SqlDataReader myReader)
{
this.myCmd=this.CreateCommand(this.myConn,para,SqlText,commandType);
try
{
myReader=myCmd.ExecuteReader();
return true;
}
catch(Exception ex)
{
return false;
}
}
//执行Sql语句返回DataSet
public virtual bool ExecuteSql(string SqlText,CommandType commandType,DataSet myDataSet)
{
this.myCmd=this.CreateCommand(this.myConn,SqlText,commandType);
this.myAdapter=new SqlDataAdapter(myCmd);
try
{
this.myAdapter.Fill(myDataSet);
return true;
}
catch(Exception ex)
{
return false;
}
}
//执行Sql语句
public virtual bool ExecuteSql(string SqlText,CommandType commandType)
{
this.myCmd=this.CreateCommand(this.myConn,SqlText,commandType);
try
{
myCmd.ExecuteNonQuery();
return true;
}
catch(Exception ex)
{
return false;
}
}
//执行Sql语句返回DataReader
public virtual bool ExecuteSql(string SqlText,CommandType commandType,SqlDataReader myReader)
{
this.myCmd=this.CreateCommand(this.myConn,SqlText,commandType);
try
{
myReader=myCmd.ExecuteReader();
return true;
}
catch(Exception ex)
{
return false;
}
}
}
}