C操作SQL数据库
11月23日 皇极城投稿 本篇文章主要讲解一下使用C语言来对SQL数据库进行【添加数据】【查询数据】【删除数据】【更新数据】相关操作
01hr
创建连接对象
1)引用命名空间usingSystem。Data。SqlC
2)连接创建的数据库,在程序加载的时候进行连接存放数据库数据源stringstrMyConnectoionDataSourceDESKTOPU6V69B6;InitialCatalog2022KIntegratedSecurityTSqlConnectionmyC数据库连接对象privatevoidForm1Load(objectsender,EventArgse){创建数据库连接对象myConnectionnewSqlConnection(strMyConnectoion);}
02hr
数据写入
在相应的输入栏里输入数据,点击【添加数据】即可将数据添加到表内
以下为源代码:privatevoidbtnAddClick(objectsender,EventArgse){try{打开数据库myConnection。Open();实例化命令对象SqlCommandmyCommandnewSqlCommand();把要操作的数据库传过来myCommand。ConnectionmyC操作类型为文本类型myCommand。CommandTypeCommandType。TmyCommand。CommandTextinsertintostudentList(学号,姓名,年龄,性别,地址)values(学号,姓名,年龄,性别,地址);myCommand。Parameters。Add(newSqlParameter(学号,textBoxID。Text));myCommand。Parameters。Add(newSqlParameter(姓名,textBoxName。Text));myCommand。Parameters。Add(newSqlParameter(年龄,textBoxAge。Text));myCommand。Parameters。Add(newSqlParameter(性别,textBoxSex。Text));myCommand。Parameters。Add(newSqlParameter(地址,textBoxAddr。Text));提交数据myCommand。ExecuteNonQuery();关闭数据库myConnection。Close();}catch(Exceptionex){MessageBox。Show(ex。ToString());myConnection。Close();}if(res){MessageBox。Show(添加成功);}}
03hr
数据查询
点击【查询数据】,可以看到表内数据被成功显示出来了
以下为源代码:privatevoidbtnFindClick(objectsender,EventArgse){selectfrom表名查询整个表select条件from表名按条件查询表格try{打开数据库myConnection。Open();实例化命令对象SqlCommandmyCommandnewSqlCommand();把要操作的数据库传过来myCommand。ConnectionmyC操作类型为文本类型myCommand。CommandTypeCommandType。T命令格式,代表查询全部myCommand。CommandTextselectfromstudentL创建DataAdapter对象SqlDataAdaptersdanewSqlDataAdapter(myCommand);创建DataSet对象DataSetdsnewDataSet();将studentList这张表填充到DataSet数据集中sda。Fill(ds,studentList);将studentList这张表显示到控件上dataGridView1。DataSourceds。Tables〔studentList〕。DefaultV关闭数据库myConnection。Close();}catch(Exceptionex){MessageBox。Show(ex。ToString());myConnection。Close();}}
04hr
按条件查询
在按姓名查询栏输入karl,点击【按姓名查询】可以看到符合条件的数据被筛选出来了
以下为源代码:privatevoidbtnCdtFindClick(objectsender,EventArgse){selectfrom表名查询整个表select列名from表名where列运算符值按条件查询表格try{打开数据库myConnection。Open();实例化命令对象SqlCommandmyCommandnewSqlCommand();把要操作的数据库传过来myCommand。ConnectionmyC操作类型为文本类型myCommand。CommandTypeCommandType。T命令格式,代表按姓名查询myCommand。CommandTextselectfromstudentListwhere姓名姓名;myCommand。Parameters。Add(newSqlParameter(姓名,textBoxCdt。Text));开始查询intresConvert。ToInt32(myCommand。ExecuteScalar());如果查询不到,报错if(res0){thrownewException(查无此人);}创建DataAdapter对象SqlDataAdaptersdanewSqlDataAdapter(myCommand);创建DataSet对象DataSetdsnewDataSet();将studentList这张表填充到DataSet数据集中sda。Fill(ds,studentList);将studentList这张表显示到控件上dataGridView1。DataSourceds。Tables〔studentList〕。DefaultV关闭数据库myConnection。Close();}catch(Exceptionex){MessageBox。Show(ex。ToString());myConnection。Close();}}
05hr
删除数据
1)点击【查询数据】查看表内数据
2)在按姓名删除栏输入karl,点击【按姓名删除】在弹出的窗口点击【确定】
3)再次点击【查询数据】可以发现姓名为karl的数据已经被删除了
以下为源代码:privatevoidbtnDelClick(objectsender,EventArgse){删除语法deletefrom表名where条件try{打开数据库myConnection。Open();实例化命令对象SqlCommandmyCommandnewSqlCommand();把要操作的数据库传过来myCommand。ConnectionmyC操作类型为文本类型myCommand。CommandTypeCommandType。T命令格式,代表按姓名查询myCommand。CommandTextdeletefromstudentListwhere姓名姓名;myCommand。Parameters。Add(newSqlParameter(姓名,textBoxDel。Text));开始查询intresConvert。ToInt32(myCommand。ExecuteNonQuery());如果查询不到,报错if(res0){thrownewException(查无此人);}MessageBox。Show(删除成功);myConnection。Close();}catch(Exceptionex){MessageBox。Show(ex。ToString());myConnection。Close();}}
06hr
将修改后的值保存到数据库
1)原来的性别栏下面的数据是男
2)将性别由男改为女,按回车,在弹出的提示对话框中点击【确定】
3)点击【确定】
4)点击【查询数据】,可以看到性别栏的原来的男被改成了女
以下为源代码:定义一个object类型的变量用来存储修改之前的数据objectcellTempV修改之前的数据privatevoiddataGridView1CellBeginEdit(objectsender,DataGridViewCellCancelEventArgse){将编辑的之前的数据存到cellTempValue变量中cellTempValuethis。dataGridView1。Rows〔e。RowIndex〕。Cells〔e。ColumnIndex〕。V}修改之后的数据privatevoiddataGridView1CellEndEdit(objectsender,DataGridViewCellEventArgse){更新语法update表名set列名值where条件如果修改前的值与修改后的值相等,直接返回不做任何操作if(object。Equals(cellTempValue,this。dataGridView1。Rows〔e。RowIndex〕。Cells〔e。ColumnIndex〕。Value)){}如果选择取消,将修改后的值还原if(MessageBox。Show(是否确定修改,并更新到数据库,提示,MessageBoxButtons。OKCancel,MessageBoxIcon。Question)!DialogResult。OK){this。dataGridView1。Rows〔e。RowIndex〕。Cells〔e。ColumnIndex〕。ValuecellTempV}如果选择确认,将修改后的值更新到数据库try{打开数据库myConnection。Open();实例化命令对象SqlCommandmyCommandnewSqlCommand();把要操作的数据库传过来myCommand。ConnectionmyC操作类型为文本类型myCommand。CommandTypeCommandType。T命令格式stringstrSqlString。Format(updatestudentListset{0}{1}where学号{2},this。dataGridView1。Columns〔e。ColumnIndex〕。HeaderText,当前选择的列名this。dataGridView1。Rows〔e。RowIndex〕。Cells〔e。ColumnIndex〕。Value,选中单元格修改后的值this。dataGridView1。Rows〔e。RowIndex〕。Cells〔0〕。Value选中单元格修改前的值);命令格式myCommand。CommandTextstrS语句执行intresmyCommand。ExecuteNonQuery();if(res0){thrownewException(修改失败);}else{MessageBox。Show(修改成功);}myConnection。Close();}catch(Exceptionex){MessageBox。Show(ex。ToString());myConnection。Close();}}
苦逼的自动化同胞们,加油!加油!加油!你离成功就差点个赞了,