[求助]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是自动增长列.
请哪位大哥帮帮助,做了几天了,不知道怎么做了,