比如有一个类,用以数据访问:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DbBase
public class Base
{
protected static string strConn = ConfigurationSettings.AppSettings["ConnectionSQLServer"];
protected static string strSQL;
public Base()
{ } public DataSet ExecuteSql4Ds(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
try
{
myCn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCn);
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);//出错时我怎么得到e.Message?
}
finally
{
myCn.Close();
}
}
}
我在WEB页面中调用它:
Base SqlBase = new Base();
string strSql="select * from table";
DataGrid1.DataSource =SqlBase.ExecuteSql4Ds (strSql);
DataGrid1.DataBind ();若不出错则正常,但若出错时,比如表名不存在,我怎么得到出错信息e.Message?让它显示在页面上的一个文本框或标签中?
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DbBase
public class Base
{
protected static string strConn = ConfigurationSettings.AppSettings["ConnectionSQLServer"];
protected static string strSQL;
public Base()
{ } public DataSet ExecuteSql4Ds(string strSQL)
{
SqlConnection myCn = new SqlConnection(strConn);
try
{
myCn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCn);
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);//出错时我怎么得到e.Message?
}
finally
{
myCn.Close();
}
}
}
我在WEB页面中调用它:
Base SqlBase = new Base();
string strSql="select * from table";
DataGrid1.DataSource =SqlBase.ExecuteSql4Ds (strSql);
DataGrid1.DataBind ();若不出错则正常,但若出错时,比如表名不存在,我怎么得到出错信息e.Message?让它显示在页面上的一个文本框或标签中?
解决方案 »
- 网页错乱问题!!!!
- 求教!为什么在MVC用模板页嵌套的网页 弹出层时页面 ,页面会滚动到最上部??
- 想用win7下的IIS发布自己用asp.net2.0做的网站,希望外网能访问
- C#做B/S系统,实现串口通信...
- 脚本问题...高手请进
- 用户登录的验证,对你们来说太初级了!
- 模糊查询.....语法
- 100分~~大家帮忙看看~
- asp.net 卡死问题???救命啊,实在没办法了
- 请教各位:把DataGrid 的数据导入word中,怎样独立启动客户端的word程序???高分请教!
- 做个 http://spaces.msn.com/ 需要多少投入~
- Infragistics控件的问题,哪位好心人帮帮忙呀!!!
不知道我拼写错了没有?
Base SqlBase = new Base();
string strSql="select * from table";
DataGrid1.DataSource =SqlBase.ExecuteSql4Ds (strSql);
DataGrid1.DataBind ();
}catch(Exception ex){
Response.Write("<script language=javascript>alert('" + ex.Message.ToString() + "')</script>");
}
catch(Exception ex)中的语句你可以自己定义,输出到文本框什么的也可以
public ResultSet ExecQuery(string sql,ReturnType returnType)
{
ResultSet rs = new ResultSet(returnType);
try
{
OpenConnection();
SqlCommand.CommandType = CommandType.Text;
SqlCommand.CommandText = sql;
SqlCommand.Connection = Conn;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql,Conn);
switch(returnType)
{
case ReturnType.DataReader :
rs.DataReader = _sqlCommand.ExecuteReader();
break;
case ReturnType.DataSet :
sqlDataAdapter = new SqlDataAdapter(_sqlCommand);
sqlDataAdapter.Fill(rs.DataSet);
break;
case ReturnType.OutputParameter :
_sqlCommand.ExecuteNonQuery();
SetOutputValue(rs);
break;
case ReturnType.SingleValue :
rs.Value = _sqlCommand.ExecuteScalar();
break;
default:
break;
}
rs.IsSucceed = true;
return rs;
}
catch(SqlException se)
{
_errorDescription = se.Message;
rs.IsSucceed = true;
return rs;
}
finally
{
CloseConnection();
}
}
/// <summary>
/// 定义数据库操作错误描述信息
/// </summary>
private string _errorDescription; /// <summary>
/// 定义数据库操作错误描述信息
/// </summary>
public string ErrorDescription
{
get
{
return _errorDescription;
}
}
逻辑层:(其中db为数据层的类,要对其进行实例化相当于你的Base类)
errorMessage = db.ErrorDescription;
{
string strerror=e.Message;//在这可以得到错误信息!
throw new Exception(e.Message);//出错时我怎么得到e.Message?
}
再
try
{}
catch()
{}