专业学的太差,在书上找到了代码和数据库·可就是调不出来,运行总出错。
发布网友
发布时间:2022-04-10 12:55
我来回答
共4个回答
热心网友
时间:2022-04-10 14:24
个人做的一个数据库类,记得引用命名空间:using wt.sql
将此代码存入App_Code中,即可引用命令空间调用。
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
/// <summary>
///DbHelperSQL 的摘要说明
/// </summary>
namespace wt.sql//类设计人:王涛
{
public class DbHelperSQL
{
#region 类的基本变量
public string ConString = "Connection String";
public SqlConnection Connection;//申明一个SqlConnection对象
#endregion
public DbHelperSQL()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public DbHelperSQL(string ConnectionString)//如果不是默认数据库,此为输入不同数据库的连接字符串参数
{
ConString = ConnectionString;
}
public void OpenConn()
{
string newConString = ConfigurationManager.ConnectionStrings[ConString].ConnectionString;
Connection = new SqlConnection(newConString);
Connection.Open();
}
#region 数据库的基本操作
//数据库操作的几个常用语句
//查询操作:select * from 表 where 字段=值 order by 字段 desc
//插入操作:insert into 表(字段1,字段2,字段3,...) values(值1,值2,值3,...)
//删除操作:delete from 表 where 字段=值
//修改操作:update 表 set 字段1=值1,字段2=值2,... where 关键字段=关键字段值
//
public DataSet GetDataSet(string sqlstring)//从数据库中得到一个数据表值
{
OpenConn();
SqlDataAdapter myda = new SqlDataAdapter(sqlstring, Connection);
DataSet myds = new DataSet();
myda.Fill(myds, "wt");
Connection.Close();
return myds;
}
public object GetSingle(string sqlstring)//从数据库中得到一个单一的值
{
OpenConn();
SqlCommand SqlCmd = new SqlCommand(sqlstring, Connection);
object Obj=SqlCmd.ExecuteScalar();
Connection.Close();
if((Object.Equals(Obj,null))||(Object.Equals(Obj,System.DBNull.Value)))
{
return null;
}
else
{
return Obj;
}
}
public int ExecuteSql(string sqlstring)//对数据库进行添加、删除、更改操作
{
OpenConn();
SqlCommand SqlCmd = new SqlCommand(sqlstring, Connection);
int rows = SqlCmd.ExecuteNonQuery();
Connection.Close();
return rows;
}
public ArrayList ReadSql(string sqlstring)//通过数组返回读出的一行值,以弥补返回SqlDataReader值时必须在调用函数后手动关闭mydr.Close()和 Connection.Close()的缺陷。缺点是只能返回一行值。
{
ArrayList mystr = new ArrayList();
OpenConn();
SqlCommand SqlCmd = new SqlCommand(sqlstring, Connection);
SqlDataReader mydr = SqlCmd.ExecuteReader();
try
{
if (mydr.Read())
{
for (int i = 0; i < mydr.FieldCount; i++)
{
mystr.Add(mydr[i]);
}
return mystr;
}
else return null;
}
finally
{
mydr.Close();
Connection.Close();
}
}
public bool CheckData(string sqlstring)//通过读取sqlstring判断数据库中有无此记录,有则返回true,没有则返回fasle
{
OpenConn();
SqlCommand mycmd = new SqlCommand( sqlstring,Connection);
SqlDataReader mydr = mycmd.ExecuteReader();
try
{
if (mydr.Read())
{
return true;
}
else
{
return false;
}
}
finally
{
mydr.Close();
Connection.Close();
}
}
#endregion
#region 常用控件的绑定
public bool control_databind(GridView gv, string mysql)//绑定GridView控件
{
OpenConn();
SqlDataAdapter myda = new SqlDataAdapter(mysql, Connection);
DataSet myds = new DataSet();
myda.Fill(myds, "wt");
gv.DataSource = myds;
try
{
gv.DataBind();
return true;
}
catch
{
return false;
}
finally
{
Connection.Close();
}
}
public bool control_databind(GridView gv, string mysql,string Datakeyname)//绑定GridView控件,且绑定一个键值
{
OpenConn();
SqlDataAdapter myda = new SqlDataAdapter(mysql, Connection);
DataSet myds = new DataSet();
myda.Fill(myds, "wt");
gv.DataSource = myds;
gv.DataKeyNames = new string[]{Datakeyname};
try
{
gv.DataBind();
return true;
}
catch
{
return false;
}
finally
{
Connection.Close();
}
}
public bool control_databind(DataList dl, string mysql)//绑定DataList控件
{
OpenConn();
SqlDataAdapter myda = new SqlDataAdapter(mysql, Connection);
DataSet myds = new DataSet();
myda.Fill(myds, "wt");
dl.DataSource = myds;
try
{
dl.DataBind();
return true;
}
catch
{
return false;
}
finally
{
Connection.Close();
}
}
public bool control_databind(DropDownList ddl, string mysql)//绑定DropDownList控件,不指定显示文字字段和值文字字段
{
OpenConn();
SqlDataAdapter myda = new SqlDataAdapter(mysql, Connection);
DataSet myds = new DataSet();
myda.Fill(myds, "wt");
ddl.DataSource = myds;
try
{
ddl.DataBind();
return true;
}
catch
{
return false;
}
finally
{
Connection.Close();
}
}
public bool control_databind(DropDownList ddl, string mysql, string Datatextfield,string Datavaluefield)//绑定DropDownList控件,指定显示文字字段和值文字字段
{
OpenConn();
SqlDataAdapter myda = new SqlDataAdapter(mysql, Connection);
DataSet myds = new DataSet();
myda.Fill(myds, "wt");
ddl.DataSource = myds;
ddl.DataTextField = Datatextfield;
ddl.DataValueField = Datavaluefield;
try
{
ddl.DataBind();
return true;
}
catch
{
return false;
}
finally
{
Connection.Close();
}
}
/// <summary>
/// 绑定TreeView(利用TreeNodeCollection)
/// </summary>
/// <param name="tnc">TreeNodeCollection(TreeView的节点集合)</param>
/// <param name="pid_val">父id的值</param>
/// <param name="id">数据库 id 字段名</param>
/// <param name="pid">数据库 父id 字段名</param>
/// <param name="text">数据库 文本 字段值</param>
private void Bind_Tv(DataTable dt, TreeNodeCollection tnc, string pid_val, string id, string pid, string text)
{
DataView dv = new DataView(dt);//将DataTable存到DataView中,以便于筛选数据
TreeNode tn;//建立TreeView的节点(TreeNode),以便将取出的数据添加到节点中
//以下为三元运算符,如果父id为空,则为构建“父id字段 is null”的查询条件,否则构建“父id字段=父id字段值”的查询条件
string filter = string.IsNullOrEmpty(pid_val) ? pid + " is null" : string.Format(pid + "='{0}'", pid_val);
dv.RowFilter = filter;//利用DataView将数据进行筛选,选出相同 父id值 的数据
foreach (DataRowView drv in dv)
{
tn = new TreeNode();//建立一个新节点(学名叫:一个实例)
tn.Value = drv[id].ToString();//节点的Value值,一般为数据库的id值
tn.Text = drv[text].ToString();//节点的Text,节点的文本显示
tnc.Add(tn);//将该节点加入到TreeNodeCollection(节点集合)中
Bind_Tv(dt, tn.ChildNodes, tn.Value, id, pid, text);//递归(反复调用这个方法,直到把数据取完为止)
}
}
#endregion
}
}
热心网友
时间:2022-04-10 15:42
给老师打电话叫他帮你弄
热心网友
时间:2022-04-10 17:17
那你是怎么写的呀
热心网友
时间:2022-04-10 19:08
把错误列表截下来,还有链接数据库的代码