问题描述
#region创建SqlDataReader对象///<summary>///创建一个SqlDataReader对象///</summary>///<paramname="M_str_sqlstr">SQL语句</param>///<returns>返回SqlDataReader对象</returns>publicSqlDataReadergetread(stringM_str_sqlstr){SqlConnectionsqlcon=this.getcon();SqlCommandsqlcom=newSqlCommand(M_str_sqlstr,sqlcon);sqlcon.Open();SqlDataReadersqlread=sqlcom.ExecuteReader(CommandBehavior.CloseConnection);returnsqlread;}#endregion
try{SqlDataReadersqlread=boperate.getread("SELECTsum(gpjg)FROMtb_gpddwhere'"+riqistart.Text.Trim()+"'<=ccrqandccrq<='"+riqiend.Text.Trim()+"'andcph='"+Convert.ToString(dgvAreaInfo[0,dgvAreaInfo.CurrentCell.RowIndex].Value).Trim()+"'");if(sqlread.Read()){dgvAreaInfo.Rows[4].Cells[4].Value=sqlread["????"].ToString();}}catch(Exceptionex){MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);}
代码如上!我调用的时候不会写dgvAreaInfo.Rows[4].Cells[4].Value=sqlread["????"].ToString();这个了!结果可以在sql里面查出来
解决方案
解决方案二:
没有指定列名只能通过下标去取你这应该是DataTable.Rows[0][0];取到
解决方案三:
sqlread.GetInt(0);//第一列的整数类型
解决方案四:
引用2楼dongxinxi的回复:
sqlread.GetInt(0);//第一列的整数类型
提示指定的转换无效
解决方案五:
gpjg是什么类型,它的sum就是什么类型,调用对应的getXXX笔法
解决方案六:
SELECTsum(gpjg)assum1FROMtb_gpdd给它指定一个列名
解决方案七:
SELECTsum(gpjg)asgpjgFROMtb_gpdd给它指定一个列名
解决方案八:
inta=dgvAreaInfo.Rows.Count;//MessageBox.Show(a.ToString());for(intb=0;b<a;b++){SqlDataReadersqlread=boperate.getread("SELECTsum(gpjg)asgpjgFROMtb_gpddwhere'"+riqistart.Text.Trim()+"'<=ccrqandccrq<='"+riqiend.Text.Trim()+"'andcph='"+Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim()+"'");sqlread.Read();if(sqlread.Read()){stringzhi1=sqlread["gpjg"].ToString();this.dgvAreaInfo.Rows[b].Cells[5].Value=zhi1.ToString();}sqlread.Close();}
解决方案九:
引用7楼criuslee的回复:
inta=dgvAreaInfo.Rows.Count;//MessageBox.Show(a.ToString());for(intb=0;b<a;b++){SqlDataReadersqlread=boperate.getread("SELECTsum(gpjg)asgpjgFROMtb_gpddwhere'"+riqistart.Text.Trim()+"'<=ccrqandccrq<='"+riqiend.Text.Trim()+"'andcph='"+Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim()+"'");sqlread.Read();if(sqlread.Read()){stringzhi1=sqlread["gpjg"].ToString();this.dgvAreaInfo.Rows[b].Cells[5].Value=zhi1.ToString();}sqlread.Close();}
这个有什么问题循环不了!
解决方案十:
引用8楼criuslee的回复:
Quote: 引用7楼criuslee的回复:
inta=dgvAreaInfo.Rows.Count;//MessageBox.Show(a.ToString());for(intb=0;b<a;b++){SqlDataReadersqlread=boperate.getread("SELECTsum(gpjg)asgpjgFROMtb_gpddwhere'"+riqistart.Text.Trim()+"'<=ccrqandccrq<='"+riqiend.Text.Trim()+"'andcph='"+Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim()+"'");sqlread.Read();if(sqlread.Read()){stringzhi1=sqlread["gpjg"].ToString();this.dgvAreaInfo.Rows[b].Cells[5].Value=zhi1.ToString();}sqlread.Close();}这个有什么问题循环不了!
是的提示在没有任何数据时。进行无效的读取尝试
解决方案十一:
自定义一个数据库类的应用实例//创建数据库类用于登陆publicclassoperateData{publicoperateData(){//TODO:在此处添加构造函数逻辑}//创建数据库连接//<returns>返回SqlConnection对象</returns>publicstaticSqlConnectioncreateCon(){//创建数据库连接SqlConnectioncon=newSqlConnection(ConfigurationManager.AppSettings["con"]);returncon;}//该方法用来实现登录查询//<paramname="sql">需要执行的SQL语句</param>//<paramname="name">登录名</param>//<paramname="pass">密码</param>//<returns>返回一个布尔值,true表示登录成功false表示失败</returns>publicstaticboollogin(stringsql,stringname,stringpass){SqlConnectioncon=createCon();con.Open();SqlCommandcom=newSqlCommand(sql,con);com.Parameters.Add(newSqlParameter("@name",SqlDbType.VarChar,20));com.Parameters["@name"].Value=name;com.Parameters.Add(newSqlParameter("@pass",SqlDbType.VarChar,20));com.Parameters["@pass"].Value=pass;intEx=Convert.ToInt32(com.ExecuteScalar());con.Close();if(Ex>0){returntrue;}else{returnfalse;}}}//codego.net/tags/11/1///调用自定义类用于登陆protectedvoidimgBtnLogin_Click(objectsender,ImageClickEventArgse){//获取用户名stringuserName=txtName.Text;//获取密码stringpass=txtPass.Text;//编写SQL语句,查询用户名和密码是否正确stringsqlSel="select*fromuserRegisterwhereusername=@nameanduserPass=@pass";//掉用自定义方法执行SQL语句if(operateData.login(sqlSel,userName,pass)){//保存用户名Session["userName"]=txtName.Text;//编写SQL语句,查询指定用户的信息stringsql="select*fromuserRegisterwhereuserName='"+Session["userName"]+"'";//调用公共类中的getRow方法,并接收该方法返回的SqlDataReader对象SqlDataReadersdr=operateData.getRow(sql);//读取一条记录sdr.Read();//判断当前用户是否被锁定if(Convert.ToBoolean(sdr["lock"])){Session["userName"]=null;RegisterStartupScript("true","<script>alert('"+sdr["lockCause"].ToString()+"');location='index.aspx'</script>");}elseRegisterStartupScript("true","<script>alert('登录成功!单击返回首页');location='index.aspx'</script>");}else{RegisterStartupScript("false","<script>alert('用户名或密码错误!')</script>");}}