ASP.NET通过SqlCommand、SqlDataReader 读取数据库问题
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(); }} 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 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); } }
[解决办法]
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.HasRows){ //加个判断,调试看看是否SqlDataReader 有数据,我觉得,你的SQL语句根本没有查询出来数据 if (dr.Read()) { this.txtname.Text = "哈哈"; } else { 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(); //我把这段挪到了ELSE里面,报错说:在没有任何数据时进行无效的读取尝试。 } con.Close(); }}
[解决办法]
1、你查得那张表有数据吗,没有数据的话,当然读不出来了。
2. string strsql = "select * from [Product] where prodID='" + productID + "'";
productID 在哪里赋值得。
[解决办法]