| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2945 人关注过本帖
标题:窗体中连接数据库,更新与添加要怎么用存储过程?
只看楼主 加入收藏
soonce
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-12-23
收藏
 问题点数:0 回复次数:6 
窗体中连接数据库,更新与添加要怎么用存储过程?
我是做好界面,将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
tanxiaolin
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2007-12-12
收藏
得分:0 
SqlDataAdapter strinsert = new SqlDataAdapter("INSERT INTO clientnew(ID,township,apellation,address,phone,few,equip,circs,buildin,hour,cattle,card,buildintime,remark) VALUES ('" + textBox4.Text + "','" + textBox6.Text + "','" + textBox5.Text + "','" + textBox17.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + textBox15.Text + "','" + textBox16.Text + "')", FPara.connStr);
                  DataSet di = new DataSet();
                  strinsert.Fill(di, "DataUserName");
我是直接这样添加添加新记录的。
2007-12-29 08:31
doughty
Rank: 1
等 级:新手上路
帖 子:372
专家分:0
注 册:2007-10-18
收藏
得分:0 
"DataUserName"这个表名是做什么的啊,,,clientnew这只是个添加没有更新啊,,,你要写个更新的存储过程,,,
2007-12-29 08:40
tanxiaolin
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2007-12-12
收藏
得分:0 
SqlDataAdapter strinsert = new SqlDataAdapter("INSERT INTO clientnew(ID,township,apellation,address,phone,few,equip,circs,buildin,hour,cattle,card,buildintime,remark) VALUES ('" + textBox4.Text + "','" + textBox6.Text + "','" + textBox5.Text + "','" + textBox17.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + textBox15.Text + "','" + textBox16.Text + "')", FPara.connStr);
                  DataSet di = new DataSet();
                  strinsert.Fill(di, "clientnew");
刚刚打错了,不好意思 呵呵。
不过也没事,我FPara.connStr更新了的。
2007-12-29 08:43
doughty
Rank: 1
等 级:新手上路
帖 子:372
专家分:0
注 册:2007-10-18
收藏
得分:0 
FPara.connStr这个没看到,,你粘上看看,,,你写存储过程没,,.看看,,还有你sqlDataAdapter1.Update(bookDS1,"bkSelectcomm");bkUpdaetcomm写的不对啊,,,
2007-12-29 08:55
soonce
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-12-23
收藏
得分:0 
这个没有怎么留意,因为这次我没有把主键也放进dataset,它是自动增长的,可能造成无法更新。我再继续找一下错误,谢谢各位的提点了。
我一开始是将SQL2005里面一个表的所有字段都绑定到窗体的文本框中,但增加新记录的时候一直说ID作为主键已经存在有值了,不过我是将ID设成CHAR类型还是INT自动增长类型都不可以,所以就想着不绑定主键。结果可以添加新记录,但对以往记录就不能更改的。
是不是缺少了主键在dataset里面,所以就无法创建Update的存储过程呢?因为后面重新创建的时候好像只生成了两个存储过程,当时没有怎么留意。
2007-12-29 09:52
tanxiaolin
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2007-12-12
收藏
得分:0 
using System;
using System.Data;
using System.Data.SqlClient;

namespace UserData
{
    public class FPara
    {
        public static string ShopCode="";
        public static bool CheckLogin()
        {
            if(ShopCode=="")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static SqlConnection connStr=new SqlConnection("Server=192.168.0.216;uid=sa;pwd=123456;database=master");
        public static SqlDataReader SqlReader(string sql,SqlConnection connstr)
        {
            SqlDataReader sqldr=null;
            SqlCommand cmd=new SqlCommand(sql,connstr);
            if (cmd.Connection.State.ToString()=="Closed") cmd.Connection.Open();
            try
            {
                sqldr=cmd.ExecuteReader();
            }
            catch(Exception e)
            {
                if (e!=null) sqldr=null;
            }
            return sqldr;
        }
        //数据库操作连接
        public static string SqlCmd(string sql,SqlConnection connstr)
        {
            string errorstr=null;
            SqlCommand sqlcmd= new SqlCommand(sql,connstr);
            if (sqlcmd.Connection.State.ToString()=="Open") sqlcmd.Connection.Close();
            sqlcmd.Connection.Open();
            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch(Exception e)
            {
                if (e!=null) errorstr=e.ToString();
            }
            sqlcmd.Connection.Close();
            return errorstr;
        }
    }
}
2007-12-29 11:10
快速回复:窗体中连接数据库,更新与添加要怎么用存储过程?
数据加载中...
 
   



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

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