c#连接数据库的增删改查

 时间:2024-11-02 06:33:40

C#基础知识

c#连接数据库的增删改查

工具/原料

电脑

vs2008+SqlServer数据库

方法/步骤

1、数据库连接好

c#连接数据库的增删改查

2、usingSystem;usingSys隋茚粟胫tem.Collections.Generic;usingSystem.ComponentM泠贾高框odel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacelogin{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=user1;IntegratedSecurity=True");try{conn.Open();//打开数据库连接//cmd.Connection=conn;//cmd.CommandText="deletefromusermessage";//cmd.ExecuteNonQuery();//stringstr="insertintousermessage(num,name,tel)values('10091','张三','3332')";stringstr="insertintousermessage(num,name,tel)values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"')";SqlCommandcmd=newSqlCommand(str,conn);cmd.ExecuteNonQuery();stringstr1="select*fromusermessagewherenum="+textBox1.Text+"";SqlCommandcmd1=newSqlCommand(str1,conn);SqlDataReaderreader=cmd1.ExecuteReader();if(reader.Read()){MessageBox.Show("添加成功!","提示");textBox1.Text="";textBox2.Text="";textBox3.Text="";}reader.Close();conn.Close();}catch(Exceptionex){MessageBox.Show("错误信息:"+ex.Message,"出现错误");}}privatevoidbutton2_Click(objectsender,EventArgse){SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=user1;IntegratedSecurity=True");try{conn.Open();stringstr1="select*fromusermessagewherenum="+textBox1.Text+"";SqlCommandcmd1=newSqlCommand(str1,conn);SqlDataReaderreader=cmd1.ExecuteReader();if(reader.Read()){reader.Close();stringstr="deletefromusermessagewherenum='"+textBox1.Text+"'";SqlCommandcmd=newSqlCommand(str,conn);cmd.ExecuteNonQuery();MessageBox.Show("删除成功!","提示");textBox1.Text="";textBox2.Text="";textBox3.Text="";conn.Close();}else{MessageBox.Show("用户不存在!","提示");textBox1.Text="";textBox2.Text="";textBox3.Text="";}}catch(Exceptionex){MessageBox.Show("错误信息:"+ex.Message,"出现错误");}}privatevoidbutton3_Click(objectsender,EventArgse){SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=user1;IntegratedSecurity=True");try{conn.Open();//打开数据库连接stringstr1="select*fromusermessagewherenum="+textBox1.Text+"";SqlCommandcmd1=newSqlCommand(str1,conn);SqlDataReaderreader=cmd1.ExecuteReader();if(reader.Read()){Stringname=reader["name"].ToString();Stringtel=reader["tel"].ToString();textBox2.Text=name.Trim();textBox3.Text=tel.Trim();}else{MessageBox.Show("用户不存在!","提示");textBox1.Text="";}reader.Close();conn.Close();}catch(Exceptionex){MessageBox.Show("错误信息:"+ex.Message,"出现错误");}}privatevoidbutton4_Click(objectsender,EventArgse){SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=user1;IntegratedSecurity=True");try{intt=0;conn.Open();if(t==0){stringstr="updateusermessagesetname='"+textBox2.Text+"',Tel='"+textBox3.Text+"'wherenum='"+textBox1.Text+"'";SqlCommandcmd=newSqlCommand(str,conn);cmd.ExecuteNonQuery();t++;;}if(t==1){stringstr1="select*fromusermessagewherenum="+textBox1.Text+"";SqlCommandcmd1=newSqlCommand(str1,conn);SqlDataReaderreader=cmd1.ExecuteReader();if(reader.Read()){Stringname=reader["name"].ToString();Stringtel=reader["tel"].ToString();textBox2.Text=name.Trim();textBox3.Text=tel.Trim();reader.Close();t=0;MessageBox.Show("修改成功!","提示");textBox1.Text="";textBox2.Text="";textBox3.Text="";}else{MessageBox.Show("用户不存在!","提示");textBox1.Text="";textBox2.Text="";textBox3.Text="";}}}catch(Exceptionex){MessageBox.Show("错误信息:"+ex.Message,"出现错误");}}privatevoidbutton5_Click(objectsender,EventArgse){textBox1.Text="";textBox2.Text="";textBox3.Text="";}privatevoiddataGridView1_CellContentClick(objectsender,DataGridViewCellEventArgse){}privatevoidbutton6_Click(objectsender,EventArgse){try{DataGridViewdataGridView1=newDataGridView();stringstrsql="select*fromusermessage";//查询语句。SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=user1;IntegratedSecurity=True");SqlCommandcmd=newSqlCommand(strsql,conn);SqlDataAdapterda=newSqlDataAdapter(cmd);DataSetds=newDataSet();da.Fill(ds);this.dataGridView1.DataSource=ds.Tables[0];this.dataGridView1.Columns[0].HeaderText="序号";this.dataGridView1.Columns[1].HeaderText="编码";this.dataGridView1.Columns[2].HeaderText="姓名";this.dataGridView1.Columns[3].HeaderText="电话";this.dataGridView1.Columns[0].Width=110;//设置该列宽度this.dataGridView1.Columns[1].Width=110;this.dataGridView1.Columns[2].Width=110;this.dataGridView1.Columns[3].Width=110;intcoun=this.dataGridView1.RowCount;for(inti=0;i<coun-1;i++){this.dataGridView1.Rows[i].Cells[0].Value=i+1;this.dataGridView1.Rows[i].Cells["id"].Value=i+1;}//默认按顺序每列DataGridView依次从ds中对应赋值this.dataGridView1.Columns[0].DataPropertyName=ds.Tables[0].Columns[0].ToString();}catch(Exceptionex){MessageBox.Show("错误信息:"+ex.Message,"出现错误");}}privatevoidForm1_Load(objectsender,EventArgse){DataGridViewdataGridView1=newDataGridView();stringstrsql="select*fromusermessage";SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=user1;IntegratedSecurity=True");SqlCommandcmd=newSqlCommand(strsql,conn);SqlDataAdapterda=newSqlDataAdapter(cmd);DataSetds=newDataSet();da.Fill(ds);this.dataGridView1.DataSource=ds.Tables[0];this.dataGridView1.Columns[0].HeaderText="序号";this.dataGridView1.Columns[1].HeaderText="编码";this.dataGridView1.Columns[2].HeaderText="姓名";this.dataGridView1.Columns[3].HeaderText="电话";this.dataGridView1.Columns[0].Width=110;//设置该列宽度this.dataGridView1.Columns[1].Width=110;this.dataGridView1.Columns[2].Width=110;this.dataGridView1.Columns[3].Width=110;intcoun=this.dataGridView1.RowCount;for(inti=0;i<coun-1;i++){this.dataGridView1.Rows[i].Cells[0].Value=i+1;this.dataGridView1.Rows[i].Cells["id"].Value=i+1;}//默认按顺序每列DataGridView依次从ds中对应赋值this.dataGridView1.Columns[0].DataPropertyName=ds.Tables[0].Columns[0].ToString();}}}

3、这是结果!!!!!!!!!!!!

c#连接数据库的增删改查

WebStorm怎么设置显示代码选择上的值工具提示 Visual Studio 2015 如何发布web应用 Oracle数据库如何设置数据源database Notepad++怎么打开所在文件夹的命令行 易语言选择列表框子程序下如何使用如果命令
热门搜索
马路图片 少女漫画在线 手抓饼图片 手拉手图片 灯泡图片