首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DB种

2013-07-01 
DB类using Systemusing System.Collections.Genericusing System.Webusing System.Data.SqlClientusin

DB类
using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
/// <summary>
///DB 的摘要说明
/// </summary>
public class DB
{
    public SqlConnection con = new SqlConnection();
    public SqlCommand cmd;
    public DataSet ds = new DataSet();
    public SqlDataAdapter sda;
    public string GetConnectionstring()
    {
        string constr = System.Configuration.ConfigurationManager.AppSettings.Get(0).ToString();
        return constr;
    }
    public DataSet GetDataTableBySql(string sqlstr)
    {
        ds.Clear();
        con.ConnectionString = GetConnectionstring();
        sda = new SqlDataAdapter(sqlstr,con);
        sda.Fill(ds);
        return ds;
    
    }
    public Boolean UpdateDataBySql(string sqlstr)
    {
        con.ConnectionString = GetConnectionstring();
        cmd = new SqlCommand(sqlstr,con);
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            return true;
        }
        catch (SqlException)
        {
            con.Close();
            return false;
        }
             
    
    }
}
请问这个DB有没有写错,如果没有的话给我讲解一些思路,谢谢!

    try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            return true;
        }
        catch (SqlException)
        {
            con.Close();
            return false;


        }



就直接返回True了。。。。意思是指要不是异常就是True 呗?


[解决办法]
public DataSet GetDataTableBySql(string sqlstr)
    {
        ds.Clear();
        con.ConnectionString = GetConnectionstring();
        sda = new SqlDataAdapter(sqlstr,con);
        sda.Fill(ds);
        return ds;
    
    }
这个函数里的ds每次最好new一个新的
[解决办法]
在这个DB类中,存在3个方法:GetConnectionstring(),GetDataTableBySql,UpdateDataBySql。GetConnectionstring()就是在配置中获取连接字符串,GetDataTableBySql就是通过sql查询语句来获取所需要的查询结果并返回,UpdateDataBySql就是根据sql语句来更新数据库表的信息。
在GetDataTableBySql中,先将DataSet 类型的实例ds先清空,然后再调用GetConnectionstring方法获取连接字符串,然后通过适配器来将查询结果保存,并用适配器中的Fill方法将结果放在ds中并返回。
在UpdateDataBySql中,也是先尝试打开连接,执行ExecuteNonQuery方法,这样就可以将sql语句执行到数据库中,然后关闭连接。
[解决办法]

     public SqlCommand cmd;
     public DataSet ds = new DataSet();
     public SqlDataAdapter sda;
/*定义在外面,每次用完后都无法及时回收,建议里面用到就在里面定义。*/

public Boolean UpdateDataBySql(string sqlstr)
     {
         con.ConnectionString = GetConnectionstring();
         cmd = new SqlCommand(sqlstr,con);
         try
         {
             con.Open();
             cmd.ExecuteNonQuery();
             con.Close();
             return true;
         }
         catch (SqlException)
         {
             con.Close();
             return false;
         }
/*不要在catch里面写con.close();在finally里面写;*/


[解决办法]
catch是捕获异常的,你把异常屏蔽了。
[解决办法]
、、、、、、、楼主可以去看看别人封装的 DBHelper 学习。
[解决办法]

引用:

     public SqlCommand cmd;
     public DataSet ds = new DataSet();
     public SqlDataAdapter sda;
/*定义在外面,每次用完后都无法及时回收,建议里面用到就在里面定义。*/

public Boolean UpdateDataBySql(string sqlstr)
     {
         con.ConnectionString = GetConnectionstring();


         cmd = new SqlCommand(sqlstr,con);
         try
         {
             con.Open();
             cmd.ExecuteNonQuery();
             con.Close();
             return true;
         }
         catch (SqlException)
         {
             con.Close();
             return false;
         }
/*不要在catch里面写con.close();在finally里面写;*/



引用:
catch是捕获异常的,你把异常屏蔽了。



能请教下哪里被屏蔽了么。。。呵呵。
[解决办法]
引用:
Quote: 引用:

public DataSet GetDataTableBySql(string sqlstr)
    {
        ds.Clear();
        con.ConnectionString = GetConnectionstring();
        sda = new SqlDataAdapter(sqlstr,con);
        sda.Fill(ds);
        return ds;
    
    }
这个函数里的ds每次最好new一个新的,为什么要new一个新麻烦解释一下,谢谢?


new个新的就是重新初始化一下咯,不然同时操作的时候会出错的。
[解决办法]
不要这样罗
public SqlConnection con = new SqlConnection();
    public SqlCommand cmd;
    public DataSet ds = new DataSet();//增,删,改不会用到,new出来干什么?浪费
    public SqlDataAdapter sda;

另外微软在PetShop中有一个SqlHelper类
你可以看看微软是怎么写的或看看别人是怎么写的

http://www.cnblogs.com/why520crazy/articles/1677281.html
http://blog.csdn.net/sabic/article/details/6633168

[解决办法]
 public DataSet GetDataTableBySql(string sqlstr)
    {
        ds=new DataSet();
        con.ConnectionString = GetConnectionstring();
        sda = new SqlDataAdapter(sqlstr,con);
        sda.Fill(ds);
        return ds;
    
    }
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

public DataSet GetDataTableBySql(string sqlstr)
    {
        ds.Clear();


        con.ConnectionString = GetConnectionstring();
        sda = new SqlDataAdapter(sqlstr,con);
        sda.Fill(ds);
        return ds;
    
    }
这个函数里的ds每次最好new一个新的,为什么要new一个新麻烦解释一下,谢谢?



new个新的就是重新初始化一下咯,不然同时操作的时候会出错的。

源错误: 


行 24:         con.ConnectionString = GetConnectionstring();
行 25:         sda = new SqlDataAdapter(sqlstr,con);
行 26:         sda.Fill(ds);
行 27:         return ds;
显示实例失败,是不是会出现这样的问题,那要在那里解决呢?


楼上已经说了。。还是建议下楼主去看看别人封装的 DBHelper 类,然后自己多用用。。
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

public DataSet GetDataTableBySql(string sqlstr)
    {
        ds.Clear();
        con.ConnectionString = GetConnectionstring();
        sda = new SqlDataAdapter(sqlstr,con);
        sda.Fill(ds);
        return ds;
    
    }
这个函数里的ds每次最好new一个新的,为什么要new一个新麻烦解释一下,谢谢?


new个新的就是重新初始化一下咯,不然同时操作的时候会出错的。

源错误: 


行 24:         con.ConnectionString = GetConnectionstring();
行 25:         sda = new SqlDataAdapter(sqlstr,con);
行 26:         sda.Fill(ds);
行 27:         return ds;
显示实例失败,是不是会出现这样的问题,那要在那里解决呢?


楼上已经说了。。还是建议下楼主去看看别人封装的 DBHelper 类,然后自己多用用。。


多用用不是指用别人封装好的。。是指自己手打,各种调用都试试,最好刚开始能 F11 跟进看看执行步骤。

热点排行
Bad Request.