c__数据库连接和操作大全.doc

上传人:sk****8 文档编号:3501563 上传时间:2019-06-01 格式:DOC 页数:7 大小:33.50KB
下载 相关 举报
c__数据库连接和操作大全.doc_第1页
第1页 / 共7页
c__数据库连接和操作大全.doc_第2页
第2页 / 共7页
c__数据库连接和操作大全.doc_第3页
第3页 / 共7页
c__数据库连接和操作大全.doc_第4页
第4页 / 共7页
c__数据库连接和操作大全.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

1、c# 数据库连接和操作大全一:数据库连接代码:SqlConnection objSqlConnection = new SqlConnection (“server = 127.0.0.1;uid = sa; pwd =;database =test“);objSqlConnection.Open();二:数据库的添加记录代码:int i = 0;string s1 = “, s2 = “;i = Convert.ToInt16(textBox1.Text);s1 = textBox2.Text;s2 = textBox3.Text;SqlConnection objSqlConnection

2、 = new SqlConnection(“server = 127.0.0.1;uid = sa; pwd =;database =test“);objSqlConnection.Open();MessageBox.Show(“数据库连接成功“, “好“);trySqlCommand sqlcom = new SqlCommand(“insert into info(id,name,sex) values( “ + i + “,“ + s1 + “,“ + s2 + “)“, objSqlConnection);sqlcom.ExecuteNonQuery();MessageBox.Show

3、(“添加成功!“, “啊“);catch (Exception a)MessageBox.Show(a.ToString();MessageBox.Show(“添加成功!“, “啊“);三:数据库的修改代码:int i = 0;string s1 = “, s2 = “;s1 = textBox2.Text;s2 = textBox3.Text;if (textBox1.Text.Length = 0)i = 0;elsei = Convert.ToInt32(textBox1.Text);SqlConnection objSqlConnection = new SqlConnection(“

4、server = 127.0.0.1;uid = sa; pwd =;database =test“);objSqlConnection.Open();MessageBox.Show(“数据库连接成功“, “好“);trySqlCommand sqlcom = new SqlCommand(“update info set name=“+s1+“,sex=“+s2+“+“where id=“ + i, objSqlConnection);sqlcom.ExecuteNonQuery();MessageBox.Show(“修改成功!“, “啊“);objSqlConnection.Close()

5、;catch (Exception a)MessageBox.Show(a.ToString();四:数据库的删除代码:int i = 0;string s1 = “, s2 = “;s1 = textBox2.Text;s2 = textBox3.Text;if(textBox1.Text.Length=0)i=0;elsei = Convert.ToInt16(textBox1.Text);SqlConnection objSqlConnection = new SqlConnection(“server = 127.0.0.1;uid = sa; pwd =;database =test

6、“); objSqlConnection.Open();MessageBox.Show(“数据库连接成功“, “好“);trySqlCommand sqlcom = new SqlCommand(“delete from info where id=“+i, objSqlConnection);sqlcom.ExecuteNonQuery();MessageBox.Show(“删除成功!“, “啊“);objSqlConnection.Close();catch (Exception a)MessageBox.Show(a.ToString();五:数据库的查询代码:1.类开始:DataTab

7、le dt1 = new DataTable();SqlDataAdapter da1 = new SqlDataAdapter();2.按钮代码:int i = 0,n=0;string s1 = “, s2 = “;s1 = textBox2.Text;s2 = textBox3.Text;if (textBox1.Text.Length = 0)i = 0;elsei = Convert.ToInt32(textBox1.Text);SqlConnection objSqlConnection = new SqlConnection(“server = 127.0.0.1;uid = s

8、a; pwd =;database =test“);objSqlConnection.Open();MessageBox.Show(“数据库连接成功“, “好“);string query = “SELECT * from info where id=“+i;DataSet objDataSet = new DataSet();SqlDataAdapter obj = new SqlDataAdapter(); obj.SelectCommand = new SqlCommand(query, objSqlConnection);obj.Fill(objDataSet, “info“); Sq

9、lCommand objSqlCommand = new SqlCommand(query, objSqlConnection);SqlDataReader objSqlReader = objSqlCommand.ExecuteReader();while (objSqlReader.Read()n += 1;MessageBox.Show(“编号: “ + objSqlReader.Getvalue(0) + “ 姓名:“ + objSqlReader.Getvalue(1) + “ 性别“ + objSqlReader.Getvalue(2);if (n = 0)MessageBox.S

10、how(“数据库中没有这样的记录!“);六:数据库的查询代码:int i = 0;/ int n = 0;string s1 = “, s2 = “;string sql;s1 = textBox2.Text;s2 = textBox3.Text;if (textBox1.Text.Length = 0)i = 0;elsei = Convert.ToInt32(textBox1.Text);SqlConnection objSqlConnection = new SqlConnection(“server = 127.0.0.1;uid = sa; pwd =;database =test“

11、); objSqlConnection.Open();MessageBox.Show(“数据库连接成功“, “好“);string query = “SELECT * from info where id=“+i;if(i=0)sql = “select * from info “;elsesql = “select * from info where id=“ + i;da1 = new SqlDataAdapter(sql, objSqlConnection);dt1.Clear();da1.Fill(dt1);dataGridView1.DataSource = dt1;数据库的封装类代

12、码:class DBClasspublic void dbclass(string sql)trySqlConnection sqlcon = new SqlConnection(“server = 127.0.0.1;uid = sa; pwd =;database =test“);sqlcon.Open();SqlTransaction objt = sqlcon.BeginTransaction(); /事物开始SqlCommand sqlcom = new SqlCommand(sql, sqlcon);sqlcom.Transaction = objt; /将 Command 对象设

13、置为事物处理的对象sqlcom.ExecuteNonQuery();objt.Commit(); /提交事物sqlcon.Close();catch (Exception a)MessageBox.Show(a.ToString();-db2 数据库连接代码:string strcon = “Provider = IBMDADB2; Data Source=hfzd;User Id=db2admin;Password=db2admin“;/string sql = “select * from ADMINISTRATOR.HFZD“;string sql = “delete from ADMINISTRATOR.HFZD where ID=1“;OleDbConnection olecon = new OleDbConnection(strcon);olecon.Open();MessageBox.Show(“数据库已连接上“);dt.Clear();da = new OleDbDataAdapter(sql, olecon);da.Fill(dt);dataGridView1.DataSource = dt;olecon.Close();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。