我是新手,从网上下了个存储过程的程序,总有一处出错,高手们帮我看一看,感激不尽。
在页面上只有一个datagrid 控件和一个按钮控件。
下面这段程序是在http://www.5ivb.net/Info/34/Info34635/上看到的。谁能帮忙看看是哪出错了,
form1.csusing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
/// 
private System.ComponentModel.Container components = null;
private Class1 pubs=new Class1();
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1; 
private DataSet ds; 
public Form1() 

// 
// Windows 窗体设计器支持所必需的 
// 
InitializeComponent(); 
// 
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
// pubs = new Publishers(); 
ds = pubs.GetPublisherInfo(); 
dataGrid1.DataSource = ds.Tables[0]; 

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
// 
// dataGrid1
// 
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 64);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(392, 112);
this.dataGrid1.TabIndex = 0;
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(264, 232);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(200, 216);
this.label1.Name = "label1";
this.label1.TabIndex = 2;
this.label1.Text = "label1";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 266);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{

} private void button1_Click(object sender, System.EventArgs e)
{

if( ds.HasChanges()) 

{

         pubs.UpdatePublisherName(ds.GetChanges()); 
     ds.Clear(); 
 ds = pubs.GetPublisherInfo();

 
} }
}
}Class1.csusing System;
using System.Data;
using System.Data.SqlClient; namespace WindowsApplication2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
private SqlConnection cnPubs; 
private SqlCommand cmdPubs; 
private SqlDataAdapter daPubs; 
private DataSet dsPubs;
        private SqlCommand cmdUpdPubs;
public Class1()
{
try 

// 创建一个数据库连接对象 
cnPubs = new SqlConnection( "server=.;integrated security=true;database=winter" ); 
// 创建一个SqlCommand对象,并指明其命令类型为存储过程 
cmdPubs = new SqlCommand(); 
cmdPubs.Connection = cnPubs; 
cmdPubs.CommandType = CommandType.StoredProcedure; 
cmdPubs.CommandText = "up4"; 
// 创建一个SqlDataAdapter对象,设定其SelectCommand属性为上面的SqlCommand对象 
daPubs = new SqlDataAdapter(); 
daPubs.SelectCommand = cmdPubs; 
// 创建一个DataSet对象 
dsPubs = new DataSet();  cmdUpdPubs = new SqlCommand(); 
cmdUpdPubs.Connection = cnPubs; 
cmdUpdPubs.CommandType = CommandType.StoredProcedure; 
cmdUpdPubs.CommandText = "up_UpdatePublisherInfo"; 
// 为上面的SqlCommand对象添加必要的参数 
cmdUpdPubs.Parameters.Add( "@pub_id", SqlDbType.Char, 4, "pub_id" ); 
cmdUpdPubs.Parameters.Add( "@pub_name", SqlDbType.VarChar, 40, "pub_name" ); 
SqlParameter updParam = new SqlParameter 
( "@Original_pub_name", SqlDbType.VarChar, 40, "pub_name" ); 
updParam.SourceVersion = DataRowVersion.Original; 
cmdUpdPubs.Parameters.Add( updParam ); 
                daPubs.UpdateCommand = cmdUpdPubs; 

catch( Exception ) {} 

public void UpdatePublisherName( DataSet dsChanges ) 

// 更新所有改动 
          
daPubs.Update( dsChanges ); 
}  public DataSet GetPublisherInfo() 

// 调用SqlDataAdapter对象的Fill()方法并返回数据集对象 
daPubs.Fill( dsPubs ); 
return dsPubs; 

}
}总是在 public void UpdatePublisherName( DataSet dsChanges ) 

// 更新所有改动 
          
daPubs.Update( dsChanges ); 

中的daPubs.Update( dsChanges ); 处出错,谁能指点一下啊。。

解决方案 »

  1.   

    mei ren hui a ,chou ren
      

  2.   

    Mark!
    太长了,能不能挑重点贴出来啊,这样没人看的
      

  3.   

    报什么错?检查一下你的数据库连接,以及SQL语句是否正确
      

  4.   

    错误信息是:未处理的“system.data.sqlclient.sqlexception”类型的异常出现在system.data.dll中。其他信息:系统错误。存储过程为:
    ALTER PROCEDURE [dbo].[up_UpdatePublisherInfo] 

    @pub_id char (4), 
    @pub_name varchar (40), 
    @city varchar (20), 
    @state char (2), 
    @country varchar (30) 

    AS 
    UPDATE publishers 
    SET pub_name = @pub_name, city = @city, state = @state,
     country = @country 
    WHERE ( pub_id = @pub_id ) 
    RETURN 点执行时,提示命令已成功完成,但是右键点执行存储过程时,提示错误信息:消息 201,级别 16,状态 4,过程 up_UpdatePublisherInfo,第 0 行
    过程或函数 'up_UpdatePublisherInfo' 需要参数 '@pub_id',但未提供该参数。(1 行受影响)这是怎么回事啊?
      

  5.   

    你的存储中用到参数“@pub_id”,但是你实际操作的时候没有给command进行添加此参数。
      

  6.   

    存储过程就有问题呀。@pub_id 是从那儿来的?这个前边要有定义。
      

  7.   

    // 为上面的SqlCommand对象添加必要的参数 
    cmdUpdPubs.Parameters.Add( "@pub_id", SqlDbType.Char, 4, "pub_id" ); 
    cmdUpdPubs.Parameters.Add( "@pub_name", SqlDbType.VarChar, 40, "pub_name" ); 
    SqlParameter updParam = new SqlParameter 
    ( "@Original_pub_name", SqlDbType.VarChar, 40, "pub_name" ); 
    updParam.SourceVersion = DataRowVersion.Original; 
    cmdUpdPubs.Parameters.Add( updParam ); 
                    daPubs.UpdateCommand = cmdUpdPubs; 不是已经给command 添加@pub_id这个参数了么?我的存储过程有问题么?
      

  8.   

    你看这里就明白了,需要对参数进行指定。
    http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
      

  9.   

    每个参数都要有具体的赋值
    System.Data.SqlClient.SqlParameter para = new SqlParameter();
    para.ParameterName = "@pub_id";
    para.Value = "AAAA";