using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Example04.Ex04_02
{
/// <summary>
/// ex04_04 的摘要说明。
/// </summary>
public class ex04_04 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblCommStr;
protected System.Web.UI.WebControls.Button btninsert;
protected System.Web.UI.WebControls.Button btndelete;
protected System.Web.UI.WebControls.Label lblFlag;
protected System.Web.UI.WebControls.Label lblcommstr;
protected System.Web.UI.WebControls.Label lblflag;
protected SqlConnection myConn=new SqlConnection(); private void Page_Load(object sender, System.EventArgs e)

{
// 在此处放置用户代码以初始化页面
//设置myconnection 的属性
string strConn="server=Localhost;uid=sa;pwd=saliensa;database=Northwind";
myConn.ConnectionString=strConn;
}
public void CreateMyCommand(String  myExecuteQuery,SqlConnection myConnection)
{
//创建数据命令,并执行
SqlCommand myCommand = new SqlCommand(myExecuteQuery,myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
//显示数据命令的commandtext属性
lblCommStr.Text="命令的CommandText 属性值为:"+myCommand.CommandText;
myConnection.Close();
}
       
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.btninsert.Click += new System.EventHandler(this.btninsert_Click);
this.btndelete.Click += new System.EventHandler(this.btndelete_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btninsert_Click(object sender, System.EventArgs e)
{
//插入记录的SQL语句
string myExecuteQuery1="insert Categories (CategoryName,Description)values('Test','Test')";
CreateMyCommand(myExecuteQuery1,myConn);
lblFlag.Text="成功插入了一条记录!";
} private void btndelete_Click(object sender, System.EventArgs e)
{
//删除记录的SQL语句
string myExecuteQuery2="delete from Categories where CategoryName like 'Test'";
CreateMyCommand(myExecuteQuery2,myConn);
lblFlag.Text="成功删除了一条记录!";
}

}

}在内部服务器浏览时出现以下提示“/Example04”应用程序中的服务器错误。
--------------------------------------------------------------------------------对象名 'Categories' 无效。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 对象名 'Categories' 无效。源错误: 
行 40:  SqlCommand myCommand = new SqlCommand(myExecuteQuery,myConnection);
行 41:  myCommand.Connection.Open();
行 42:  myCommand.ExecuteNonQuery();
行 43:  //显示数据命令的commandtext属性
行 44:  lblCommStr.Text="命令的CommandText 属性值为:"+myCommand.CommandText;
 源文件: c:\inetpub\wwwroot\example04\ex04_02\ex04_04.aspx.cs    行: 42 

解决方案 »

  1.   


    你是说这段吗?堆栈跟踪: 
    [SqlException: 对象名 'Categories' 无效。]
       System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +182
       Example04.Ex04_02.ex04_04.CreateMyCommand(String myExecuteQuery, SqlConnection myConnection) in c:\inetpub\wwwroot\example04\ex04_02\ex04_04.aspx.cs:42
       Example04.Ex04_02.ex04_04.btninsert_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\example04\ex04_02\ex04_04.aspx.cs:75
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain() +1292 
      

  2.   

    "server=Localhost;uid=sa;pwd=saliensa;database=Northwind"
    确定这个northwind里有?
      

  3.   

    好像在引用时,要加一个所有者DBO,即使用insert dbo.Categories (CategoryName,Description)values('Test','Test')
      

  4.   

    private void btninsert_Click(object sender, System.EventArgs e)
            {
                //插入记录的SQL语句
                string myExecuteQuery1="insert Categories (CategoryName,Description)values('Test','Test')";
                CreateMyCommand(myExecuteQuery1,myConn);
                lblFlag.Text="成功插入了一条记录!";
            }应该是insert into 吧