C#中如何实现数据源的添加和删除

我用的C#和SQL做一个数据库管理,但现在做出来能显示数据库中的内容就是无法实现对数据源的添加和删除,BindingNavigator和DataGridView如何才能实... 我用的C# 和SQL做一个数据库管理,但现在做出来能显示数据库中的内容就是无法实现对数据源的添加和删除,BindingNavigator和DataGridView 如何才能实现对数据源的添加和删除并成功显示出来 展开
 我来答
dong_1984dd
推荐于2016-02-08 · TA获得超过1.1万个赞
知道小有建树答主
回答量:1070
采纳率:100%
帮助的人:223万
展开全部
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;
}
匿名用户
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"]);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
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的内容不一样。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-09-09
展开全部
insertcommand,deletecommand等几个方法里面写事件就行了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式