那位高手帮忙看下了 如果把报错语句改为 SqlCommand myCmd = dbObj.GetDataSetStr(strAddSql,"tbBI");则会报这样的错 
无法将类型“System.Data.DataTable”隐式转换为“System.Data.SqlClient.SqlCommand”
下面是后台代码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 Manage_ProductAdd : System.Web.UI.Page
{
    CommonClass ccObj = new CommonClass();
    DBClass dbObj = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlClassBind();   //绑定商品类别
            ddlUrlBind();     //绑定商品供选图像
        }
    }
    public void ddlClassBind()
    {
        string strSql = "select * from tb_Class";
        DataTable dsTable = dbObj.GetDataSetStr(strSql, "tbClass");
        //将商品类别信息绑定到DropDownList控件中
        this.ddlCategory.DataSource = dsTable.DefaultView;
        this.ddlCategory.DataTextField = dsTable.Columns[1].ToString();//绑定商品类别名
        this.ddlCategory.DataValueField = dsTable.Columns[0].ToString();//绑定商品类别号
        this.ddlCategory.DataBind();
    }
    public void ddlUrlBind()
    {
        string strSql = "select * from tb_Image";
        DataTable dsTable = dbObj.GetDataSetStr(strSql, "tbImage");
        this.ddlUrl.DataSource = dsTable.DefaultView;
        this.ddlUrl.DataTextField = dsTable.Columns[1].ToString();
        this.ddlUrl.DataValueField = dsTable.Columns[2].ToString();
        this.ddlUrl.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
       int IntClassID=Convert.ToInt32(this.ddlCategory.SelectedValue.ToString());//商品类别号
        string strBookName=this.txtName.Text.Trim();                      //商品类别名
        string strBookDesc=this.txtShortDesc.Text.Trim();                 //商品简短描述
        string strAuthor=this.txtAuthor.Text.Trim();                      //书籍作者
        string strCompany=this.txtCompany.Text.Trim();                    //书籍出版社
        string strBookUrl=this.ddlUrl.SelectedValue.ToString();           //商品图像路径
        float fltMarketPrice=float.Parse(this.txtMarketPrice.Text.Trim());//商品市场价
        float fltHotPrice=float.Parse(this.txtHotPrice.Text.Trim());      //商品热销价
        bool blrefine =Convert.ToBoolean(this.cbxCommend.Checked);        //是否推价
        bool blHot = Convert.ToBoolean(this.cbxHot.Checked);              //是否热销
        bool blDiscount = Convert.ToBoolean(this.cbxDiscount.Checked);//是否打折
        string strSql = "select * from tb_BookInfo where BookName='" + strBookName + "'and Author='" + strAuthor + "'and Company='" + strCompany + "'";
        DataTable dsTable=dbObj.GetDataSetStr(strSql,"tbBI");
        if(dsTable.Rows.Count>0)
        {
             Response.Write(ccObj.MessageBox("该商品已经存在!"));
        }
        else
        {//将商品信息插入数据库中
            string strAddSql="Insert into tb_BookInfo(ClassID,BookName,BookIntroduce,Author,Company,BookUrl,MarketPrice,HotPrice,Isrefinement,IsHot,IsDiscount)";
            strAddSql += "values ('" + IntClassID + "','" + strBookName + "','" + strBookDesc + "','" + strAuthor + "','" + strCompany + "','" + strBookUrl + "','" + fltMarketPrice + "','" + fltHotPrice + "','" + blrefine + "','" + blHot + "','" + blDiscount + "')";
            SqlCommand myCmd = dbObj.GetDataSetStr(strAddSql);
            dbObj.ExecNonQuery(myCmd);
            Response.Write(ccObj.MessageBox("添加成功!"));
        
        }    }
protected void  btnReset_Click(object sender, EventArgs e)
{
     this.txtName.Text = "";
        this.txtAuthor.Text = "";
        this.txtCompany.Text = "";
        this.txtMarketPrice.Text = "";
        this.txtHotPrice.Text = "";
        this.txtShortDesc.Text = "";
}    protected void ddlUrl_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.ImageMapPhoto.ImageUrl = ddlUrl.SelectedItem.Value;
    }
}

解决方案 »

  1.   

    dbObj.GetDataSetStr(strAddSql,"tbBI"); 这里很显示返回的是一个dataTable你用一个SqlCommand对象去接收,能不错吗?
      

  2.   


    谢谢你的回复  我放接触.NET  麻烦你详细说下 
      

  3.   

    SqlCommand myCmd = dbObj.GetDataSetStr(strAddSql,"tbBI");
    改为:
    DataTable myTb = dbObj.GetDataSetStr(strAddSql,"tbBI");
      

  4.   

     SqlCommand myCmd = dbObj.GetDataSetStr(strAddSql);
    //你调用dbObj.GetDataSetStr(strAddSql);方法会返回一个DataTable,怎么能够赋值给一个Sqlcommand呢。
      SqlCommand myCmd = dbObj.GetDataSetStr(strAddSql);
      dbObj.ExecNonQuery(myCmd);改为:
      SqlCommand myCmd = new SqlCommand(strAddSql,conn);
      myCmdExecNonQuery();//自己新建一个SqlConnction对象。conn
      

  5.   

    SqlCommand表示要对 SQL Server 数据库执行的一个 Transact-SQL 语句或存储过程,而DataTable是执行查询的时候得到的一个结果,二者怎么可能相等的?你还是去msdn上看看基础的类介绍吧,先了解这些类是干嘛的
      

  6.   


    谢谢 我是刚接触.NET  正在学 以后多多指教
      

  7.   

    dbObj.GetDataSetStr返回datatable
    DataTable dt=dbObj.GetDataSetStr(strAddSql,"tbBI");
      

  8.   

    如果这样写了 
    那下边 执行存储的时候dbObj.ExecNonQuery(myCmd);
    就不对了 
    与“DBClass.ExecNonQuery(System.Data.SqlClient.SqlCommand)”最匹配的重载方法具有一些无效参数
      
      

  9.   

    SqlCommand myCmd = dbObj.GetDataSetStr(strAddSql);
     
    确实问题