C#中如何实现数据源的添加和删除
我用的C#和SQL做一个数据库管理,但现在做出来能显示数据库中的内容就是无法实现对数据源的添加和删除,BindingNavigator和DataGridView如何才能实...
我用的C# 和SQL做一个数据库管理,但现在做出来能显示数据库中的内容就是无法实现对数据源的添加和删除,BindingNavigator和DataGridView 如何才能实现对数据源的添加和删除并成功显示出来
展开
5个回答
展开全部
1.参考下列C# 代码调用MYSQL 数据库,数据库用的是本地服务器(MySql):
2.设定全局变量:
MySqlConnection conn;
MySqlDataAdapter adapter;
MySqlTransaction trans;
3. // 数据库联接
private System.Data.DataTable dbconn(string strSql)
{
string strconn = "host=localhost;database=test;user id=root;password=";
conn = new MySqlConnection();
conn.ConnectionString = strconn;
conn.Open();
this.adapter = new MySqlDataAdapter(strSql, conn);
System.Data.DataTable dtSelect = new System.Data.DataTable();
int rnt=this.adapter.Fill(dtSelect);
conn.Close();
return dtSelect;
}
4. //设定DataGridView的样式
private void setDgStyle()
{
this.dgselect.Columns.Clear();
DataGridViewCheckBoxColumn colDel = new DataGridViewCheckBoxColumn();
colDel.DataPropertyName = "Del";
colDel.Name = "Del";
colDel.Selected = false;
colDel.FalseValue = "0";
colDel.TrueValue = "1";
colDel.Width = 40;
colDel.SortMode = DataGridViewColumnSortMode.NotSortable;
colDel.HeaderText = "删除";
colDel.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colDel.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dgselect.Columns.Insert(0, colDel);
DataGridViewTextBoxColumn colID = new DataGridViewTextBoxColumn();
colID.DataPropertyName = "ProductsSpecID";
colID.Name = "ProductsSpecID";
colID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colID.HeaderText = "产品规格ID";
colID.Width = 160;
this.dgselect.Columns.Insert(1, colID);
DataGridViewTextBoxColumn colNM = new DataGridViewTextBoxColumn();
colNM.DataPropertyName = "ProductsSpec";
colNM.Name = "ProductsSpec";
colNM.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colNM.HeaderText = "产品规格名称";
colNM.Width = 160;
this.dgselect.Columns.Insert(2, colNM);
DataGridViewTextBoxColumn colUnit = new DataGridViewTextBoxColumn();
colUnit.DataPropertyName = "ProductsSpecUnit";
colUnit.Name = "ProductsSpecUnit";
colUnit.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colUnit.HeaderText = "产品规格单位";
colUnit.Width = 180;
this.dgselect.Columns.Insert(3, colUnit);
DataGridViewTextBoxColumn colPID = new DataGridViewTextBoxColumn();
colPID.DataPropertyName = "ProductsID";
colPID.Name = "ProductsID";
colPID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colPID.HeaderText = "产品ID";
colPID.Width = 140;
this.dgselect.Columns.Insert(4, colPID);
DataGridViewButtonColumn colButton = new DataGridViewButtonColumn();
colButton.DataPropertyName = "colSearch";
colButton.Name = "colSearch";
colButton.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colButton.HeaderText = "Button";
colButton.Width = 80;
this.dgselect.Columns.Insert(5, colButton);
this.dgselect.RowHeadersWidth = 15;
this.dgselect.ColumnHeadersDefaultCellStyle.Font=new System.Drawing.Font("宋体",14);
}
5. //修改数据,并将数据提交到数据库
private Boolean dbUpdate()
{
string strSql = "select ProductsSpecID,ProductsSpec,ProductsSpecUnit,ProductsID from tbl_product_detail_master";
System.Data.DataTable dtUpdate = new System.Data.DataTable();
dtUpdate = this.dbconn(strSql);
dtUpdate.Rows.Clear();
System.Data.DataTable dtShow = new System.Data.DataTable();
//dtShow = (DataTable)this.bindSource.DataSource;
dtShow = (System.Data.DataTable)this.dgselect.DataSource;
int p1 = dtShow.Rows.Count;
// try
// {
for (int i = 0; i < dtShow.Rows.Count; i++)
{
DataRowState rowState=new DataRowState();
rowState=dtShow.Rows[i].RowState;
if (rowState==DataRowState.Added || rowState==DataRowState.Detached || rowState==DataRowState.Modified)
{
if (this.dgselect["Del", i].Value.ToString() == "1")
{
dtShow.Rows[i].Delete();
}
}
}
for (int i = 0; i < dtShow.Rows.Count; i++)
{
dtUpdate.ImportRow(dtShow.Rows[i]);
}
int num = dtUpdate.Rows.Count;
try
{
this.conn.Open();
trans = this.conn.BeginTransaction();
MySqlCommandBuilder CommandBuiler;
CommandBuiler = new MySqlCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate);
trans.Commit();
this.conn.Close();
}
catch ( Exception ex)
{
MessageBox.Show(ex.Message.ToString());
trans.Rollback();
return false;
}
dtUpdate.AcceptChanges();
return true;
}
2.设定全局变量:
MySqlConnection conn;
MySqlDataAdapter adapter;
MySqlTransaction trans;
3. // 数据库联接
private System.Data.DataTable dbconn(string strSql)
{
string strconn = "host=localhost;database=test;user id=root;password=";
conn = new MySqlConnection();
conn.ConnectionString = strconn;
conn.Open();
this.adapter = new MySqlDataAdapter(strSql, conn);
System.Data.DataTable dtSelect = new System.Data.DataTable();
int rnt=this.adapter.Fill(dtSelect);
conn.Close();
return dtSelect;
}
4. //设定DataGridView的样式
private void setDgStyle()
{
this.dgselect.Columns.Clear();
DataGridViewCheckBoxColumn colDel = new DataGridViewCheckBoxColumn();
colDel.DataPropertyName = "Del";
colDel.Name = "Del";
colDel.Selected = false;
colDel.FalseValue = "0";
colDel.TrueValue = "1";
colDel.Width = 40;
colDel.SortMode = DataGridViewColumnSortMode.NotSortable;
colDel.HeaderText = "删除";
colDel.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colDel.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dgselect.Columns.Insert(0, colDel);
DataGridViewTextBoxColumn colID = new DataGridViewTextBoxColumn();
colID.DataPropertyName = "ProductsSpecID";
colID.Name = "ProductsSpecID";
colID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colID.HeaderText = "产品规格ID";
colID.Width = 160;
this.dgselect.Columns.Insert(1, colID);
DataGridViewTextBoxColumn colNM = new DataGridViewTextBoxColumn();
colNM.DataPropertyName = "ProductsSpec";
colNM.Name = "ProductsSpec";
colNM.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colNM.HeaderText = "产品规格名称";
colNM.Width = 160;
this.dgselect.Columns.Insert(2, colNM);
DataGridViewTextBoxColumn colUnit = new DataGridViewTextBoxColumn();
colUnit.DataPropertyName = "ProductsSpecUnit";
colUnit.Name = "ProductsSpecUnit";
colUnit.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colUnit.HeaderText = "产品规格单位";
colUnit.Width = 180;
this.dgselect.Columns.Insert(3, colUnit);
DataGridViewTextBoxColumn colPID = new DataGridViewTextBoxColumn();
colPID.DataPropertyName = "ProductsID";
colPID.Name = "ProductsID";
colPID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colPID.HeaderText = "产品ID";
colPID.Width = 140;
this.dgselect.Columns.Insert(4, colPID);
DataGridViewButtonColumn colButton = new DataGridViewButtonColumn();
colButton.DataPropertyName = "colSearch";
colButton.Name = "colSearch";
colButton.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colButton.HeaderText = "Button";
colButton.Width = 80;
this.dgselect.Columns.Insert(5, colButton);
this.dgselect.RowHeadersWidth = 15;
this.dgselect.ColumnHeadersDefaultCellStyle.Font=new System.Drawing.Font("宋体",14);
}
5. //修改数据,并将数据提交到数据库
private Boolean dbUpdate()
{
string strSql = "select ProductsSpecID,ProductsSpec,ProductsSpecUnit,ProductsID from tbl_product_detail_master";
System.Data.DataTable dtUpdate = new System.Data.DataTable();
dtUpdate = this.dbconn(strSql);
dtUpdate.Rows.Clear();
System.Data.DataTable dtShow = new System.Data.DataTable();
//dtShow = (DataTable)this.bindSource.DataSource;
dtShow = (System.Data.DataTable)this.dgselect.DataSource;
int p1 = dtShow.Rows.Count;
// try
// {
for (int i = 0; i < dtShow.Rows.Count; i++)
{
DataRowState rowState=new DataRowState();
rowState=dtShow.Rows[i].RowState;
if (rowState==DataRowState.Added || rowState==DataRowState.Detached || rowState==DataRowState.Modified)
{
if (this.dgselect["Del", i].Value.ToString() == "1")
{
dtShow.Rows[i].Delete();
}
}
}
for (int i = 0; i < dtShow.Rows.Count; i++)
{
dtUpdate.ImportRow(dtShow.Rows[i]);
}
int num = dtUpdate.Rows.Count;
try
{
this.conn.Open();
trans = this.conn.BeginTransaction();
MySqlCommandBuilder CommandBuiler;
CommandBuiler = new MySqlCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate);
trans.Commit();
this.conn.Close();
}
catch ( Exception ex)
{
MessageBox.Show(ex.Message.ToString());
trans.Rollback();
return false;
}
dtUpdate.AcceptChanges();
return true;
}
2013-09-09
展开全部
直接拖表进来,就是一个Gridview,要是表有主键的话,增删改查只要启用就行了。不然对一个表进行操作很麻烦,也没必要,因为你做的是个系统,那么多表肯定有联系,必然有主键。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-09
展开全部
DataGridView对数据源的添加和删除非常简单,。如果你有个保存修改的按钮,,在它的单击事件里面写上:public partial class ExamForm2 : Form
{
public ExamForm2()
{
InitializeComponent();
} private void ExamForm2_Load(object sender, EventArgs e)
{
loadInfo();
} DataSet ds = new DataSet();
SqlDataAdapter da = null; private void loadInfo()
{
string sql = string.Format("select id,nickName,sex,age from users order by id");
try
{
da = new SqlDataAdapter(sql, DBHelper.con);
da.Fill(ds, "users");
dgvUsers.DataSource = ds.Tables["users"];
}
catch (Exception)
{ throw;
}
finally
{
DBHelper.con.Close();
}
} private void button1_Click(object sender, EventArgs e) //修改保存事件
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.Update(ds.Tables["users"]);
}
}
{
public ExamForm2()
{
InitializeComponent();
} private void ExamForm2_Load(object sender, EventArgs e)
{
loadInfo();
} DataSet ds = new DataSet();
SqlDataAdapter da = null; private void loadInfo()
{
string sql = string.Format("select id,nickName,sex,age from users order by id");
try
{
da = new SqlDataAdapter(sql, DBHelper.con);
da.Fill(ds, "users");
dgvUsers.DataSource = ds.Tables["users"];
}
catch (Exception)
{ throw;
}
finally
{
DBHelper.con.Close();
}
} private void button1_Click(object sender, EventArgs e) //修改保存事件
{
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.Update(ds.Tables["users"]);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-09
展开全部
string str1="Data Source=.;Initial Catalog=Job;Integrated Security=true"; SqlConnection dbconnection = new SqlConnection(str1);dbconnection .Open();SqlCommand deletecommand = new SqlCommand();deletecommand.CommandText = " 删除记录的T-SQL语句";
deletecommand.Connection = dbconnection;deletecommand.ExecuteNonQuery();//以上为删除string sqlstring = "select 要显示的字段名 from 要显示的表名";
viewsda = new SqlDataAdapter(sqlstring, dbconnection);
viewds = new DataSet();
adapter.Fill(viewds,"viewinfor");
dataGridView1.DataSource = viewds;
dataGridView1.DataMember = "viewinfor";//以上为dataGridView内容显示。添加的方法和删除一样,只是T-SQL的内容不一样。
deletecommand.Connection = dbconnection;deletecommand.ExecuteNonQuery();//以上为删除string sqlstring = "select 要显示的字段名 from 要显示的表名";
viewsda = new SqlDataAdapter(sqlstring, dbconnection);
viewds = new DataSet();
adapter.Fill(viewds,"viewinfor");
dataGridView1.DataSource = viewds;
dataGridView1.DataMember = "viewinfor";//以上为dataGridView内容显示。添加的方法和删除一样,只是T-SQL的内容不一样。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-09
展开全部
insertcommand,deletecommand等几个方法里面写事件就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |