| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 867 人关注过本帖
标题:[求助]winform请问怎么往数据库中增加值?
只看楼主 加入收藏
liuming2008
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-6
收藏
 问题点数:0 回复次数:3 
[求助]winform请问怎么往数据库中增加值?
有一个班级管理的窗体,在DataGridView中显示数据库中的信息,
在按下添加弹出一个窗体用于添加一个新的班级名和开班日期,确定添加成功.
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace Class
{
    public partial class frmclass : Form
    {
        public SqlConnection objconn;
        public SqlDataAdapter objsqlda;
        public DataSet objdataset = new DataSet();
        public SqlParameter objsqlpt = new SqlParameter();
        public DialogResult objdr;

        public frmclass()
        {
            InitializeComponent();
        }

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

        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmclass_Load(object sender, EventArgs e)
        {
            objconn = new SqlConnection("server=.;database=NetStuDemo;uid=sa;pwd=");
            objsqlda = new SqlDataAdapter("select * from Class",objconn);

            objsqlda.Fill(objdataset,"Class");
            dbgclass.DataSource = objdataset.Tables[0].DefaultView;
        }

        /// <summary>
        /// 添加显示另外一个窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnadd_Click(object sender, EventArgs e)
        {
            addclass ac = new addclass();
            ac.Show();
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btndelet_Click(object sender, EventArgs e)
        {
            string delcmd = "delete from class where classid=@classid";
            objconn.Open();
            objsqlda.DeleteCommand = new SqlCommand(delcmd, objconn);
            objsqlpt = objsqlda.DeleteCommand.Parameters.Add("@classid",SqlDbType.Char);
            objsqlpt.SourceColumn = "classid";
            objsqlpt.SourceVersion = DataRowVersion.Original;

            objdr = MessageBox.Show("你确定要删除当前记录吗?","确认",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (objdr == DialogResult.Yes)
            {
                try
                {
                    objdataset.Tables[0].Rows[dbgclass.CurrentRow.Index].Delete();
                    objsqlda.Update(objdataset, "class");
                    MessageBox.Show("已删除记录");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
                return;
        }
    }
}

这是添加的窗体
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace Class
{
    public partial class addclass : Form
    {
        public SqlConnection objconn = new SqlConnection("server=.;database=NetStuDemo;uid=sa;pwd=");
        public SqlDataAdapter objsqlda=new SqlDataAdapter();
        public DataSet objdataset = new DataSet();
        public SqlParameter objsqlpt = new SqlParameter();
        public DialogResult objdr;

        public addclass()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string inscmd = "insert into Class values(@classname,@BeginDate)";
                objconn.Open();
                objsqlda.InsertCommand = new SqlCommand(inscmd, objconn);
               
                objsqlpt = objsqlda.InsertCommand.Parameters.Add("@classname", SqlDbType.Char);
                objsqlpt.SourceColumn = "classname";
                objsqlpt.SourceVersion = DataRowVersion.Current;

                objsqlpt = objsqlda.InsertCommand.Parameters.Add("@BeginDate", SqlDbType.DateTime);
                objsqlpt.SourceColumn = "BeginDate";
                objsqlda.Fill(objdataset,"Class");
                objsqlpt.SourceVersion = DataRowVersion.Current;

                if (objdataset.HasChanges())
                {
                    try
                    {
                        objsqlda.Update(objdataset, "class");
                        MessageBox.Show("已插入记录");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

还有一个班级ID是自动增长列.
请哪位大哥帮帮助,做了几天了,不知道怎么做了,
搜索更多相关主题的帖子: using 数据库 增加值 System winform 
2008-04-27 00:14
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
- -|
看到图像...以为是静老大的呢...

msdn == 葵花宝典!!!
QQ:122768959
2008-04-27 09:49
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
还有...提问请言简意赅...
看了半天..还不知道你想问什么呢

msdn == 葵花宝典!!!
QQ:122768959
2008-04-27 09:51
liuming2008
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-6
收藏
得分:0 
不好意思,我就是在执行到添加按钮单击确定的时候出现异常.
SqlCommand未初始化
单步调试到 objsqlda.Fill(objdataset,"Class");这里就发生异常了

2008-04-27 10:33
快速回复:[求助]winform请问怎么往数据库中增加值?
数据加载中...
 
   



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

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