C#与sqlserver数据库操作-附实例说明(共5页).doc

上传人:飞****2 文档编号:5436949 上传时间:2022-01-07 格式:DOC 页数:5 大小:24.50KB
返回 下载 相关 举报
C#与sqlserver数据库操作-附实例说明(共5页).doc_第1页
第1页 / 共5页
C#与sqlserver数据库操作-附实例说明(共5页).doc_第2页
第2页 / 共5页
点击查看更多>>
资源描述

《C#与sqlserver数据库操作-附实例说明(共5页).doc》由会员分享,可在线阅读,更多相关《C#与sqlserver数据库操作-附实例说明(共5页).doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上C#数据库连接操作大全下面是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

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

3、 ), objSqlConnection);sqlcom.ExecuteNonQuery(); MessageBox.Show(添加成功!, 啊); 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.Te

4、xt);SqlConnection objSqlConnection = new SqlConnection(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.S

5、how(修改成功!, 啊);objSqlConnection.Close();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;else i = Convert.ToInt16(textBox1.Text);SqlConnection objSqlConnection = new SqlConnection(server = 127.

6、0.0.1;uid = sa; pwd =;database =test);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();

7、五:数据库的查询代码:1.类开始:DataTable 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(serv

8、er = 127.0.0.1;uid = sa; 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(objDataSe

9、t, info);SqlCommand 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)MessageBo

10、x.Show(数据库中没有这样的记录!);六:数据库的查询代码: 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;数据库的封装类代码:class DBC

12、lasspublic void dbclass(stringsql)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 对象设置为事物处理的对象sqlcom.

13、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();软件开发的路很辛苦,希望各位兄弟姐妹好好坚持下去,这是一条不错的路,相信自己。专心-专注-专业

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

当前位置:首页 > 应用文书 > 教育教学

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁