下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上C#数据库连接操作大全下面是c#与数据库的连接及增删改除的各种操作,全部经过上机验证。本人是从事软件开发,如果各位有什么问题或者需要帮助的可以在我的空间留言。一:数据库连接代码:SqlConnection objSqlConnection = new SqlConnection ("server = ;uid = sa; pwd =;database =test");objSqlConnection.Open();二:数据库的添加记录代码:
2、0; int i = 0; string s1 = "", s2 = "" i = Convert.ToInt16(textBox1.Text);
3、 s1 = textBox2.Text; s2 = textBox3.Text; SqlConnection objSqlConnection = new SqlConnection("server = ;uid = sa; pwd =;database =test"
4、); objSqlConnection.Open(); MessageBox.Show("数据库连接成功", "好");
5、 try SqlCommand sqlcom = new S
6、qlCommand("insert into info(id,name,sex) values( " + i + ",'" + s1 + "','" + s2 + "')", objSqlConnection); sqlcom.ExecuteNonQuery();
7、; MessageBox.Show("添加成功!", "啊"); &
8、#160; catch (Exception a) MessageBox.Show(a.ToString();
9、; MessageBox.Show("添加成功!", "啊");三:数据库的修改代码: int i = 0;
10、; string s1 = "", s2 = "" s1 = textBox2.Text; s2 = textBox3.Text;
11、160; if (textBox1.Text.Length = 0) i = 0; else
12、; i = Convert.ToInt32(textBox1.Text); SqlConnection objSqlConnection = new SqlConnection("server = ;uid = sa; pwd =;database =test");
13、 objSqlConnection.Open(); MessageBox.Show("数据库连接成功", "好"); try
14、; SqlCommand sqlcom = new SqlCommand("update info set name='"+s1+"',sex='"+s2+"'"+"where id=" + i, objSqlConnection);
15、160; sqlcom.ExecuteNonQuery(); MessageBox.Show("修改成功!", "啊");
16、; objSqlConnection.Close(); catch (Exception a)
17、60; MessageBox.Show(a.ToString(); 四:数据库的删除代码: int i
18、 = 0; string s1 = "", s2 = "" s1 = textBox2.Text; s2 = textBox3.Tex
19、t; if(textBox1.Text.Length=0) i=0; else
20、 i = Convert.ToInt16(textBox1.Text); SqlConnection objSqlConnection = new SqlConnection("server = ;uid = sa; pwd =;database =test");
21、 objSqlConnection.Open(); MessageBox.Show("数据库连接成功", "好");
22、160; try SqlCommand sqlcom = new SqlCommand("delete from info where id=&quo
23、t;+i, objSqlConnection); sqlcom.ExecuteNonQuery(); MessageBox.Show("删除成功!", "啊");
24、60; objSqlConnection.Close(); catch (Exception a) &
25、#160; MessageBox.Show(a.ToString(); 五:数据库的查询代码:
26、60; 1.类开始: DataTable dt1 = new DataTable(); SqlDataAdapter da1
27、= new SqlDataAdapter(); 2.按钮代码: int i = 0,n=0; string s1 = "", s2 = "&qu
28、ot; s1 = textBox2.Text; s2 = textBox3.Text; if (textBox1.Text.Length = 0) &
29、#160; i = 0; else i = Convert.ToInt32(textBox1.Text);
30、0; SqlConnection objSqlConnection = new SqlConnection("server = ;uid = sa; pwd =;database =test"); objSqlConnection.Open();
31、0; MessageBox.Show("数据库连接成功", "好"); string query = "SELECT * from info where id="+i; DataSet
32、objDataSet = new DataSet(); SqlDataAdapter obj = new SqlDataAdapter(); obj.SelectCommand = new Sql
33、Command(query, objSqlConnection); obj.Fill(objDataSet, "info"); SqlCommand objSqlCommand = new SqlCo
34、mmand(query, objSqlConnection); SqlDataReader objSqlReader = objSqlCommand.ExecuteReader(); while (objSqlReader.Read()
35、60; n += 1;
36、160;MessageBox.Show("编号: " + objSqlReader.Getvalue(0) + " 姓名:" + objSqlReader.Getvalue(1) + " 性别" + objSqlReader.Getvalue(2);
37、60; if (n = 0) MessageBox.Show("数据库中没有这样的记录!");六:数据库的查询代码: int i = 0;&
38、#160; / int n = 0; string s1 = "", s2 = "" string sql;
39、 s1 = textBox2.Text; s2 = textBox3.Text; if (textBox1.Text.Length = 0)
40、; i = 0;
41、; else i = Convert.ToInt32(textBox1.Text); SqlConnection objSqlConnection = new
42、SqlConnection("server = ;uid = sa; pwd =;database =test"); objSqlConnection.Open(); &
43、#160;MessageBox.Show("数据库连接成功", "好"); string query = "SELECT * from info where id="+i; if(i=0)
44、0; sql = "select * from info " else sql =
45、 "select * from info where id=" + i; da1 = new SqlDataAdapter(sql, objSqlConnection); dt1.Clear();
46、160; da1.Fill(dt1); dataGridView1.DataSource = dt1;数据库的封装类代码:class DBClass public void dbclass(string sql)
47、 try SqlConnection sqlcon = new SqlCon
48、nection("server = ;uid = sa; pwd =;database =test"); sqlcon.Open();
49、; SqlTransaction objt = sqlcon.BeginTransaction(); /事物开始 SqlCom
50、mand sqlcom = new SqlCommand(sql, sqlcon); sqlcom.Transaction = objt; /将Command 对象设置为事物处理的对象
51、; sqlcom.ExecuteNonQuery(); objt.Commit(); /提交事物 sqlcon.Close();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高三英语二轮复习+Extension+ Test+应用文写作课件
- 资源置换合作协议
- 2024教师个人实习报告(33篇)
- 教师聚会活动方案
- 教师实习心得实践教案1000字(32篇)
- 幼儿园区域游戏材料投放及教师有效介入培训学习心得
- 2024年现场水质仪器项目合作计划书
- 【人教版】二年级数学下册导学案设计《综合与实践 小小设计师》
- 2024年血细胞分析仪器试剂项目建议书
- 《 北京版《格斯尔传》语言研究》范文
- 2024年全国职业院校技能大赛高职组(化工生产技术赛项)考试题库-中(多选题)
- (初级)航空油料飞机加油员(五级)理论考试题库(含答案)
- 良好学习习惯养成月实施方案
- 2024义务教育数学新课标课程标准2022版考试真题及答案
- 麦肯锡:2024中国汽车消费者洞察报告(英文版)-2024-08-市场解读
- 2019年高考政治专题复习:经济生活曲线图题
- 中华联合保险集团股份有限公司行测笔试题库2024
- 2024年部编版初中语文各年级教师用书七年级(上册)
- 2024年河南养老护理员职业技能竞赛理论考试题库(附答案)
- 小学中队长竞选竞聘
- 地质灾害危险性评估收费标准
评论
0/150
提交评论