在C#中使用控件DataGridView实现数据库增删改查

本文介绍了一个使用 C# 进行数据库操作的示例,包括如何连接数据库、查询数据、插入记录、更新记录及删除记录等基本操作。通过这个示例可以了解如何在 Windows Forms 应用程序中实现对数据库的基本 CRUD 操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;

namespaceDataSource
...{
publicpartialclassForm1:Form
...{
publicForm1()
...{
InitializeComponent();
}


privateDataSetds=newDataSet();
privateSqlConnectionconn=null;
privateSqlDataAdapterda=null;
privateconststringDRIVER="server=.;database=northwind;uid=sa;pwd=sa";
privateconststringsql_select="select*fromregion";

/**//**
*此方法为将数据库northwind中的region表的数据查询出来并放入DataSet中
*
*/

privatevoidForm1_Load(objectsender,EventArgse)
...{
conn
=newSqlConnection(DRIVER);
da
=newSqlDataAdapter(sql_select,conn);
da.Fill(ds,
"table");
this.dataGridView1.DataSource=ds.Tables["table"].DefaultView;
}


privateboolBtnInsert()//此方法作用于添加
...{
da.InsertCommand
=conn.CreateCommand();
da.InsertCommand.CommandText
="insertintoregionvalues(@id,@ption)";
da.InsertCommand.Parameters.Add(
"@id",SqlDbType.Int,4,"regionid");
da.InsertCommand.Parameters.Add(
"@ption",SqlDbType.VarChar,10,"regiondescription");
intcount=da.Update(ds);
boolresult=count>0?true:false;
returnresult;
}

privatevoidbutton1_Click(objectsender,EventArgse)
...{
if(this.BtnInsert())//调用此方法
...{
MessageBox.Show(
"添加成功!");
}

else
...{
MessageBox.Show(
"添加失败!");
}

}



privateboolBtnDelect()//此方法作用于删除
...{
SqlParametersp
=newSqlParameter();
da.DeleteCommand
=conn.CreateCommand();
da.DeleteCommand.CommandText
="deleteregionwhereregionid=@id";
sp
=da.DeleteCommand.Parameters.Add("@id",SqlDbType.Int,4,"regionid");
sp.SourceVersion
=DataRowVersion.Original;
ds.Tables[
"table"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
intcount=da.Update(ds);
boolresult=count>0?true:false;
returnresult;
}

privatevoidbutton2_Click(objectsender,EventArgse)
...{
if(this.BtnDelect())//调用删除方法
...{
MessageBox.Show(
"删除成功!");
}

else
...{
MessageBox.Show(
"删除失败!");
}

}



privateboolBtnUpdate()//此方法作用于修改
...{
SqlParametersp
=newSqlParameter();
da.UpdateCommand
=conn.CreateCommand();
da.UpdateCommand.CommandText
="updateregionsetregionid=@id,regiondescription=@ptionwhereregionid=@oldid";

da.UpdateCommand.Parameters.Add(
"@id",SqlDbType.Int,4,"regionid");
da.UpdateCommand.Parameters.Add(
"@ption",SqlDbType.VarChar,10,"regiondescription");

sp
=da.UpdateCommand.Parameters.Add("@oldid",SqlDbType.Int,4,"regionid");
sp.SourceVersion
=DataRowVersion.Original;

intcount=da.Update(ds);
boolresult=count>0?true:false;
returnresult;
}

privatevoidbutton3_Click(objectsender,EventArgse)
...{
if(this.BtnUpdate())//调用修改方法
...{
MessageBox.Show(
"修改成功!");
}

else
...{
MessageBox.Show(
"修改失败!");
}

}



}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值