当前位置: 代码迷 >> ASP.NET >> ASP.NET通过SqlCommand、SqlDataReader 读取数据库有关问题
  详细解决方案

ASP.NET通过SqlCommand、SqlDataReader 读取数据库有关问题

热度:2628   发布时间:2013-02-25 00:00:00.0
ASP.NET通过SqlCommand、SqlDataReader 读取数据库问题
C# code
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class admin_zpxg : System.Web.UI.Page{    SqlConnection con;    string sqlcon = ConfigurationManager.ConnectionStrings["rjs_admin"].ConnectionString;    private static int productID;    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            productID = Convert.ToInt32(Request.QueryString["prodID"]);            contentdatabind();        }    }    protected void contentdatabind()    {        con = new SqlConnection(sqlcon);        con.Open();        string strsql = "select * from [Product] where prodID='" + productID + "'";        SqlCommand cmd = new SqlCommand(strsql,con);        SqlDataReader dr = cmd.ExecuteReader();        if (dr.Read())        {            //this.txtname.Text = dr["prodName"].ToString();            //this.ddlgroup.Text = dr["groupName"].ToString();            //this.txtimgpath.Text = dr["prodImages"].ToString();            //this.prodauthor.Text = dr["prodAuthor"].ToString();            //this.prodintroduce.Text = dr["prodIntroduce"].ToString();            this.txtname.Text = "哈哈";            //我测试了下,确定是Read没有读取,把上面的注释去掉的话会报错、说是没有读取到。。。        }        else        {            this.txtname.Text = "啊啊啊啊";        }        con.Close();    }}


------解决方案--------------------------------------------------------
string strsql = "select * from [Product] where prodID='" + productID + "'";

这里加断点,然后看strsql值是什么?
------解决方案--------------------------------------------------------
逐步排查下把 。
------解决方案--------------------------------------------------------
C# code
 con = new SqlConnection(sqlcon);        con.Open();        string strsql = "select * from [Product] where prodID='" + productID + "'";         SqlDataAdapter sda = new SqlDataAdapter(strsql,con );           DataSet ds=new  DataSet();         sda.fill(ds);           if (ds.table[0].rows.count>0)        {                       this.txtname.Text = ds.table[0].rows[0]["prodName"].TOString();           }        else        {            this.txtname.Text = "啊啊啊啊";        }        con.Close();
------解决方案--------------------------------------------------------
探讨

引用:
string strsql = "select * from [Product] where prodID='" + productID + "'";

这里加断点,然后看strsql值是什么?

我以前也遇到过这个情况,但是用try catch能行就没管,这次是要发誓解开心里的疙瘩啊啊啊~`

------解决方案--------------------------------------------------------
打端点调试吧。。。一步一步看
------解决方案--------------------------------------------------------
因为存在null值,所以读不出来,是先判断一下是否为null
标准的做法

C# code
        string strsql = "select prodName,groupName,prodImages,prodAuthor,prodIntroduce from [Product] where prodID='1'";        SqlCommand cmd = new SqlCommand(strsql,con);        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))        {            if (dr.Read())            {                this.txtname.Text = dr.IsDBNull(0) ? string.Empty : dr.GetString(0);                this.ddlgroup.Text = dr.IsDBNull(1) ? string.Empty : dr.GetString(1);                this.txtimgpath.Text = dr.IsDBNull(2) ? string.Empty : dr.GetString(2);                this.prodauthor.Text = dr.IsDBNull(3) ? string.Empty : dr.GetString(3);                this.prodintroduce.Text = dr.IsDBNull(4) ? string.Empty : dr.GetString(4);            }        }
  相关解决方案