数据库的连接问题
来源:互联网 发布:谢谢的网络用语2017 编辑:程序博客网 时间:2024/04/30 14:12
1.在配置文件里面 <configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=myWebDB;
UID=sa;pwd=123321"></add>
</appSettings> <connectionStrings/>
在页面后台里面
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings
["ConnectionString"]);
myConnection.Open();
2.直接在一个类里面写,然后调用这个连接
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace MyOfficeBBL
{
public class GetConnection
{
private static SqlConnection _connection;
/// <summary>
/// 获取数据库连接桥
/// </summary>
private static SqlConnection Connection
{
get
{
string connectionString = "Data Source=.;Initial Catalog=MyOffice;Integrated Security=True";
if (_connection == null)
{
_connection = new SqlConnection(connectionString);
_connection.Open();
}
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
if (_connection.State == ConnectionState.Broken)
{
_connection.Close();
_connection.Open();
}
return GetConnection._connection;
}
}
/// <summary>
/// 获取表数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static SqlDataReader GetDataRead(string sql)
{
SqlCommand command = new SqlCommand(sql, Connection);
SqlDataReader read = command.ExecuteReader();
return read;
}
public static int NoSelect(string sql)
{
SqlCommand command = new SqlCommand(sql, Connection);
int row = command.ExecuteNonQuery();
return row;
}
public static DataTable GetDataTable(string sql)
{
SqlCommand command = new SqlCommand(sql, Connection);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(command);
sda.Fill(dt);
return dt;
}
}
}
- 数据库的连接问题
- 数据库的连接池问题
- 数据库的连接池问题
- 数据库的再次连接问题
- 连接数据库的小问题
- java连接数据库的问题
- powerdesigner连接数据库的问题
- powerdesigner连接数据库的问题
- 连接数据库遇到的问题
- 数据库的连接数问题
- 数据库远程连接的问题
- Ado连接数据库的问题
- 数据库的左连接,右连接问题
- 关于jsp连接oracle数据库的问题
- 新手请教jsp连接数据库的问题
- 关于 Hibernate 连接数据库的问题
- 一个ReportNet连接Oracle数据库的问题
- 用JAVA连接ORACLE数据库的问题
- delphi 2010编译exe文件大小
- InstallShield常用常量
- 如何为IDE注册第三方Silverlight控件
- Too many connections错误的解决办法
- 一个Windows C++的线程类实现
- 数据库的连接问题
- Linux export 命令
- 你是如何让position:fixed在IE6中工作的?
- android中去掉标题栏和状态栏,实现全屏
- 百万数据查询优化技巧三十则(从朋友那转来的)
- 第三章 t-sql程序设计基础 批处理与用户自定义函数
- Zend Framework 项目目录结构
- ArrayList Array List性能比较
- 研究