博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
后台连接数据库的方法
阅读量:4567 次
发布时间:2019-06-08

本文共 993 字,大约阅读时间需要 3 分钟。

1.直接写数据库连接字符串

string constr = "server=.;database=myschool;integrated security=SSPI";//string constr = "server=.;database=myschool;uid=sa;pwd=sa";//string constr = "data source=.;initial catalog=myschool;user id=sa;pwd=sa";SqlConnection con = new SqlConnection(constr);// con.ConnectionString = constr;string sql = "select count(*) from grade";SqlCommand com = new SqlCommand(sql,con);try{      con.Open();      MessageBox.Show("成功连接数据库");      int x = (int)com.ExecuteScalar();      MessageBox.Show(string.Format("成功读取{0},条记录", x));}catch (Exception){      throw;}finally{      con.Close();      MessageBox.Show("成功关闭数据库连接", "提示信息", MessageBoxButtons.YesNoCancel);}

2.web.config配置

C#后台处理程序

string connectionString =ConfigurationManager.ConnectionStrings["myschool"].ConnectionString;
 
private string strCon = ConfigurationManager.AppSettings["ConnectionString"];

 

转载于:https://www.cnblogs.com/9968jie/p/8075545.html

你可能感兴趣的文章
Ansi,UTF8,Unicode编码
查看>>
原子变量的性能问题
查看>>
Sybase PowerDesigner 15.0 完美版+特别文件
查看>>
快速傅立叶之二
查看>>
cetos 6.3 安装 apache+mysql+php
查看>>
js编写简单的贪吃蛇游戏
查看>>
2018/12/01 一个64位操作系统的实现 第四章 导入kernel.bin(4)
查看>>
如何在windows xp professional安装iis的解决方法
查看>>
抽象类和接口
查看>>
使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(下)
查看>>
golang 常见疑惑总结
查看>>
8大你不得不知的Android调试工具
查看>>
pc端元素拖拽
查看>>
Sublime Text3使用Package Control 报错There Are No Packages Available For Installation
查看>>
判断连通图是否有环(并查集)
查看>>
汽车之家面试题2016
查看>>
POJ-数据结构-优先队列模板
查看>>
【HAOI2006】旅行(并查集暴力)
查看>>
css实现文本超出部分省略号显示
查看>>
留言板
查看>>