标题:[求助]winform请问怎么往数据库中增加值?
取消只看楼主
liuming2008
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-6
 问题点数:0 回复次数:1 
[求助]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
liuming2008
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-6
得分:0 
不好意思,我就是在执行到添加按钮单击确定的时候出现异常.
SqlCommand未初始化
单步调试到 objsqlda.Fill(objdataset,"Class");这里就发生异常了

2008-04-27 10:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-211276-1-1.html




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

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