| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2945 人关注过本帖
标题:窗体中连接数据库,更新与添加要怎么用存储过程?
取消只看楼主 加入收藏
soonce
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-12-23
收藏
 问题点数:0 回复次数:1 
窗体中连接数据库,更新与添加要怎么用存储过程?
我是做好界面,将sqldataadapter控件拖到窗体上,使用生成新的存储过程命令
分别是
bkSelectcomm
bkDeletecomm
bkUpdaetcomm
bkInsertcomm

具体在以下的程序中应该怎么用到?
下面我做的程序只可以添加新记录,但是却不能更改,说找不到update命令。
各位大哥,小弟初学C#,用的是VS2005,请帮帮我啊~

public partial class Book : Form
    {
        public Book()
        {
            InitializeComponent();
            sqlConnection1.Open();
            sqlDataAdapter1.Fill(bookDS1,"bkSelectcomm");
            CurrentPosition();
        }

        private void CurrentPosition()
        {
            int currentPostion, ctr;
            ctr = this.BindingContext[bookDS1, "bkSelectcomm"].Count;
            if (ctr == 0)
            {
                txtPosition.Text = "目前没有记录";
            }
            else
            {
                currentPostion = this.BindingContext[bookDS1, "bkSelectcomm"].Position + 1;
                txtPosition.Text = currentPostion.ToString() + "of" + ctr.ToString();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                btnBack.BindingContext[bookDS1, "bkSelectcomm"].Position -= 1;
                CurrentPosition();
            }
            catch (System.Exception eBack)
            {
                System.Windows.Forms.MessageBox.Show(eBack.ToString());
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                btnNext.BindingContext[bookDS1, "bkSelectcomm"].Position += 1;
                CurrentPosition();
            }
            catch (System.Exception eNext)
            {
                System.Windows.Forms.MessageBox.Show(eNext.ToString());
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                this.BindingContext[bookDS1, "bkSelectcomm"].EndCurrentEdit();
                this.BindingContext[bookDS1, "bkSelectcomm"].AddNew();

            }
            catch (System.Exception eEndEdit)
            {
                System.Windows.Forms.MessageBox.Show(eEndEdit.Message);
            }
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            textBox5.Enabled = true;
            textBox6.Enabled = true;
            textBox7.Enabled = true;
            textBox8.Enabled = true;
            textBox9.Enabled = true;
            //int i=this.BindingContext[bookDS1, "bkSelectcomm"].Position;
            //textBox1.Text = bookDS1.Tables[0].Rows[i]["BookID"].ToString();
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            
            //textBox1.Text = bookDS1.Tables[0].Rows[this.BindingContext[bookDS1, "bkSelectcomm"].Position][0].ToString();
            CurrentPosition();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            textBox6.Enabled = true;
            textBox7.Enabled = true;
            textBox8.Enabled = true;
            textBox9.Enabled = true;
            bookDS1.Tables[0].Rows[this.BindingContext[bookDS1, "bkSelectcomm"].Position][0] = textBox2.Text;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                this.BindingContext[bookDS1,"bkSelectcomm"].RemoveAt(this.BindingContext[bookDS1,"bkSelectcomm"].Position);
                CurrentPosition();
            }
            catch (System.Exception eRemove)
            {
                System.Windows.Forms.MessageBox.Show(eRemove.ToString());
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            bookDS1.Clear();
            sqlDataAdapter1.Fill(bookDS1,"bkSelectcomm");
            CurrentPosition();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            sqlDataAdapter1.Update(bookDS1,"bkSelectcomm");
            MessageBox.Show("Update");
        }
    }
搜索更多相关主题的帖子: 数据库 窗体 public Book bkSelectcomm 
2007-12-29 01:53
soonce
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-12-23
收藏
得分:0 
这个没有怎么留意,因为这次我没有把主键也放进dataset,它是自动增长的,可能造成无法更新。我再继续找一下错误,谢谢各位的提点了。
我一开始是将SQL2005里面一个表的所有字段都绑定到窗体的文本框中,但增加新记录的时候一直说ID作为主键已经存在有值了,不过我是将ID设成CHAR类型还是INT自动增长类型都不可以,所以就想着不绑定主键。结果可以添加新记录,但对以往记录就不能更改的。
是不是缺少了主键在dataset里面,所以就无法创建Update的存储过程呢?因为后面重新创建的时候好像只生成了两个存储过程,当时没有怎么留意。
2007-12-29 09:52
快速回复:窗体中连接数据库,更新与添加要怎么用存储过程?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024693 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved