ps2 bios-python多线程
![datagridview选中行](/uploads/image/0515.jpg)
2023年4月3日发(作者:网易云心动模式是什么)
C#通过DataGridView对数据库进⾏增删改查
运⾏效果.png
⼀、准备数据库Student,数据表TableStudent
CREATETABLE[dbo].[TableStudent](
[stuId]INTIDENTITY(1,1)NOTNULL,
[stuName]NVARCHAR(32)NULL,
[stuSex]NCHAR(2)NULL,
[stuBirthdate]NVARCHAR(32)NULL,
[stuPhone]NVARCHAR(32)NULL,
PRIMARYKEYCLUSTERED([stuId]ASC)
);
⼆、获取数据库连接字符串
usingSystem;
c;
;
;
;
uration;
ent;
;
namespaceDataAdapterExample
{
classsqlHelper
{
//获取数据库连接字符串
publicstaticstringGetConnectionString()
{
tionStrings["strConnect"].ConnectionString;
}
#region封装⼀个执⾏SQL返回受影响的⾏数
publicstaticintExecuteNoQuery(stringsqlText,paramsSqlParameter[]parameters)
{
using(SqlConnectioncon=newSqlConnection(GetConnectionString()))
{
using(SqlCommandcmd=Command())
{
{
();
dText=sqlText;
ge(y());
eNonQuery();
}
}
}
#endregion
#region封装⼀个执⾏SQL返回查询结果中第⼀⾏第⼀列的值
publicstaticobjectExecuteScalar(stringsqlText,paramsSqlParameter[]parameters)
{
using(SqlConnectioncon=newSqlConnection(GetConnectionString()))
{
using(SqlCommandcmd=Command())
{
();
dText=sqlText;
ge(y());
eScalar();
}
}
}
#endregion
#region封装⼀个执⾏SQL返回⼀个DataTable
publicstaticDataTableExecuteDataTable(stringsqlText,paramsSqlParameter[]parameters)
{
using(SqlDataAdapteradapter=newSqlDataAdapter(sqlText,GetConnectionString()))
{
DataTabledt=newDataTable();
ge(y());
(dt);
returndt;
}
}
#endregion
#region封装⼀个执⾏SQL返回⼀个SqlDataReader
publicstaticSqlDataReaderExecutedReader(stringsqlText,paramsSqlParameter[]parameters)
{
//SqlDataReader要求独占SqlConnection对象,并且SqlConnection必须是Open状态
SqlConnectioncon=newSqlConnection(GetConnectionString());
();
SqlCommandcmd=Command();
dText=sqlText;
ge(y());
//SqlDataReader执⾏完成后顺便关闭数据库连接
eReader(onnection);
}
#endregion
}
}
三、数据表映射StudentInfo帮助类
usingSystem;
c;
;
;
;
namespaceDataAdapterExample
{
publicclassStudentInfo
{
publicintstuId{get;set;}
publicstringstuName{get;set;}
publicstringstuSex{get;set;}
publicstringstuBirthdate{get;set;}
publicstringstuPhone{get;set;}
}
}
四、主窗体对数据表进⾏CURD
usingSystem;
c;
entModel;
;
g;
;
;
;
;
uration;
ent;
namespaceDataAdapterExample
{
publicpartialclassMainFrm:Form
{
//标识正在修改的数据⾏的主键
privateintupdateStudentId=0;
publicMainFrm()
{
InitializeComponent();
}
//主窗体加载数据
privatevoidMainFrm_Load(objectsender,EventArgse)
{
LoadStudentInfo();
}
//增加数据
privatevoidButtonAdd_Click(objectsender,EventArgse)
{
using(SqlConnectioncon=newSqlConnection(nectionString()))
{
();
using(SqlCommandcmd=Command())
{
dText="insertintoTableStudent(stuName,stuPhone,stuBirthdate,stuSex)values(@stuName,@stuPhone,@stuBirthdate,@stuSex)";
hValue("@stuName",());
hValue("@stuPhone",());
hValue("@stuSex",());
hValue("@stuBirthdate",());
if(eNonQuery()>0)
{
("增加数据成功!");
}
}
}
LoadStudentInfo();
}
//删除数据
privatevoidButtonDelete_Click(objectsender,EventArgse)
{
if(<=0)
{
("请选择要删除的数据");
}
if(("确认要删除吗?","提醒消息",,g)!=)
{
return;
}
intdeleteStuId=(edRows[0].Cells["stuId"].ng());
#region原⽣⼿写
//using(SqlConnectioncon=newSqlConnection(nectionString()))
//{
//();
//stringsql="deletefromTableStudentwherestuId=@stuId";
//using(SqlCommandcmd=Command())
//{
//dText=sql;
//hValue("stuId",deleteStuId);
////("@stuId",);
////ters["stuId"].Value=deleteStuId;
////SqlParameterparameter=newSqlParameter();
////terName="@stuId";
////=deleteStuId;
////(parameter);
//if(eNonQuery()>0)
//{
//("删除成功");
//}
//}
//}
#endregion
#regionsqlHelper封装⽅法ExecuteNoQuery
stringstrSQL="deletefromTableStudentwherestuId=@stuId";
intnum=eNoQuery(strSQL,newSqlParameter("@stuId",(object)deleteStuId));
if(num>0)
{
("删除成功");
}
#endregion
LoadStudentInfo();
}
//修改数据
privatevoidbuttonUpdate_Click(objectsender,EventArgse)
{
using(SqlConnectioncon=newSqlConnection(nectionString()))
{
using(SqlCommandcmd=Command())
{
();
dText="updateTableStudentsetstuName=@stuName,stuSex=@stuSex,stuBirthdate=@stuBirthdate,stuPhone=@stuPhonewherestuId=@stuId";
hValue("@stuName",);
hValue("@stuBirthdate",);
hValue("@stuSex",);
hValue("@stuPhone",);
hValue("@stuId",StudentId);
if(eNonQuery()>0)
{
("更新数据成功");
}
}
}
//刷新数据
//刷新数据
ButtonSearch_Click(this,null);
}
//选中数据赋值到对应⽂本框
privatevoiddataGridViewFromAdapter_SelectionChanged(objectsender,EventArgse)
{
if(<=0)
{
//("请先选中数据");
return;
}
//选中⾏的Id
intselectedId=(edRows[0].Cells["stuId"].ng());
//把要修改的数据⾏的ID放到当前窗体的updateStudentId字段中保存
updateStudentId=selectedId;
using(SqlConnectioncon=newSqlConnection(nectionString()))
{
();
using(SqlCommandcmd=Command())
{
dText="selectstuId,stuName,stuSex,stuBirthdate,stuPhonefromTableStudentwherestuId=@stuId";
hValue("@stuId",selectedId);
using(SqlDataReaderreader=eReader())
{
if(())
{
=reader["stuSex"].ToString().Trim();
=reader["stuName"].ToString().Trim();
=reader["stuBirthdate"].ToString().Trim();
=reader["stuPhone"].ToString().Trim();
}
}
}
}
}
//双击弹出修改窗体
privatevoiddataGridViewFromAdapter_DoubleClick(objectsender,EventArgse)
{
if(<=0)
{
return;
}
inteditStuId=(edRows[0].Cells["stuId"].ng());
EditStudentFormeditStuFrm=newEditStudentForm(newStudentInfo(){stuId=editStuId});
//注册弹出窗体的关闭事件
osing+=EditStuFrm_FormClosing;
();
}
//弹出窗体修改完成关闭的时候执⾏的操作
privatevoidEditStuFrm_FormClosing(objectsender,FormClosingEventArgse)
{
LoadStudentInfo();
}
//多条件查询
privatevoidButtonSearch_Click(objectsender,EventArgse)
{
//拼接whereSqlText脚本
stringwhereSqlText="selectstuId,stuName,stuPhone,stuBirthdate,stuSexfromTableStudent";
List
List
if(!OrEmpty(()))
{
//把where条件添加到whereList集合中
("stuNamelike@stuName");
SqlParameterparameter=newSqlParameter();
terName="@stuName";
="%"+()+"%";
="%"+()+"%";
(parameter);
}
if(!OrEmpty(()))
{
//把where条件添加到whereList集合中
("stuSexlike@stuSex");
SqlParameterparameter=newSqlParameter();
terName="@stuSex";
="%"+()+"%";
(parameter);
}
if(>0)
{
whereSqlText+="where"+("and",whereList);
//(whereSqlText);
}
//加载数据⽅法的调⽤
LoadStudentInfo2DataGridView(whereSqlText,y());
}
//加载数据
privatevoidLoadStudentInfo()
{
List
stringsqlText="selectstuId,stuName,stuSex,stuBirthdate,stuPhonefromTableStudent";
LoadStudentInfo2DataGridView(sqlText);
}
//加载数据⽅法的封装
publicvoidLoadStudentInfo2DataGridView(stringsqlText,paramsSqlParameter[]parameters)
{
List
#region原⽣写法
//using(SqlConnectioncon=newSqlConnection(nectionString()))
//{
//using(SqlDataAdapteradapter=newSqlDataAdapter(sqlText,con))
//{
////填充之前,给SelectCommand赋参数
//ge(y());
//DataTabledt=newDataTable();
//(dt);
//foreach()
//{
//StudentInfostudentInfo=newStudentInfo();
//=(row["stuId"].ToString().Trim());
//e=row["stuName"].ToString().Trim();
//=row["stuSex"].ToString().Trim();
//thdate=row["stuBirthdate"].ToString().Trim();
//ne=row["stuPhone"].ToString().Trim();
//(studentInfo);
//}
//urce=studentInfoList;
//}
//}
#endregion
#regionsqlHelper类的ExecuteDataTable⽅法
DataTabledt=eDataTable(sqlText,parameters);
foreach()
{
StudentInfostudentInfo=newStudentInfo();
=(row["stuId"].ToString().Trim());
e=row["stuName"].ToString().Trim();
=row["stuSex"].ToString().Trim();
thdate=row["stuBirthdate"].ToString().Trim();
ne=row["stuPhone"].ToString().Trim();
(studentInfo);
}
urce=studentInfoList;
#endregion
}
}
}
五、双击DataGridView选中⾏弹出⼦窗体EditStudentForm对数据可以进⾏修改
usingSystem;
c;
entModel;
;
g;
;
;
;
;
ent;
namespaceDataAdapterExample
{
publicpartialclassEditStudentForm:Form
{
//窗体属性
publicStudentInfoStuInfo{get;set;}
//构造函数接收StudentInfo对象
publicEditStudentForm(StudentInfostuInfo)
{
InitializeComponent();
//将传递的对象赋值给当前对象的属性
StuInfo=stuInfo;
}
//弹出窗体的时候加载传递⾏stuId对应的数据到对应⽂本框
privatevoidEditStudentForm_Load(objectsender,EventArgse)
{
#region原⽣写法
//using(SqlConnectioncon=newSqlConnection(nectionString()))
//{
//using(SqlCommandcmd=Command())
//{
//();
//dText="selectstuSex,stuName,stuBirthdate,stuPhonefromTableStudentwherestuId=@stuId";
//hValue("@stuId",);
//using(SqlDataReaderreader=eReader())
//{
//if(())
//{
//=reader["stuBirthdate"].ToString().Trim();
//=reader["stuName"].ToString().Trim();
//=reader["stuPhone"].ToString().Trim();
//=reader["stuSex"].ToString().Trim();
//}
//}
//}
//}
#endregion
#regionsqlHelper类的ExecuteReader⽅法
stringstrSQL="selectstuSex,stuName,stuBirthdate,stuPhonefromTableStudentwherestuId=@stuId";
using(SqlDataReaderreader=edReader(strSQL,newSqlParameter("@stuId",(object))))
{
if(())
{
=reader["stuBirthdate"].ToString().Trim();
=reader["stuName"].ToString().Trim();
=reader["stuPhone"].ToString().Trim();
=reader["stuSex"].ToString().Trim();
}
}
#endregion
}
//保存更新
privatevoidButtonSave_Click(objectsender,EventArgse)
{
#region原⽣写法
//using(SqlConnectioncon=newSqlConnection(nectionString()))
//{
//using(SqlCommandcmd=Command())
//{
//();
//dText="updateTableStudentsetstuPhone=@stuPhone,stuName=@stuName,stuBirthdate=@stuBirthdate,stuSex=@stuSexwherestuId=@stuId";
//hValue("@stuId",);
//hValue("@stuName",());
//hValue("@stuBirthdate",());
//hValue("@stuPhone",());
//hValue("@stuSex",());
//if(eNonQuery()>0)
//{
//("更新成功");
//}
////关闭窗体
//();
//}
//}
#endregion
#region使⽤sqlHelper类的ExecuteNoQuery静态⽅法
stringstrSQL="updateTableStudentsetstuPhone=@stuPhone,stuName=@stuName,stuBirthdate=@stuBirthdate,stuSex=@stuSexwherestuId=@stuId";
List
SqlParameterparaStuName=newSqlParameter("@stuName",ar,32);
=();
(paraStuName);
SqlParameterparaStuSex=newSqlParameter("@stuSex",ar,32);
=();
(paraStuSex);
SqlParameterparaStuBirthdate=newSqlParameter("@stuBirthdate",ar,32);
=();
(paraStuBirthdate);
SqlParameterparaStuPhone=newSqlParameter("@stuPhone",ar,32);
=();
(paraStuPhone);
SqlParameterparaStuId=newSqlParameter("@stuId",ar,32);
=;
(paraStuId);
intnum=eNoQuery(strSQL,y());
if(num>0)
{
("保存更新成功!");
}
//关闭窗⼝
();
#endregion
}
}
}
主窗体最下⾯的⽂本框功能:
(1)增加新的数据⾏
(2)显⽰选中⾏数据
(3)修改选中⾏数据
优化:sqlHelper类中封装⽅法
(1)执⾏SQL返回受影响的⾏数的ExecuteNoQuery⽅法
(2)执⾏SQL返回查询结果中第⼀⾏第⼀列的值的ExecuteScalar⽅法
(3)执⾏SQL返回⼀个DataTable的ExecuteDataTable⽅法
(4)执⾏SQL返回⼀个SqlDataReader的ExecutedReader⽅法
更多推荐
datagridview选中行
发布评论