.net/c#连接sqlserver
Webconfig代码
<configuration>
<appSettings>
<add key="myconnect" value="server=.;UID=sa;password='1';database=test" />
</appSettings>
</configuration>
数据库类代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
///DB_class 的摘要说明
/// </summary>
public class DB_class
{
SqlConnection myconn;
SqlCommand mycmd;
DataSet ds; //数据集
SqlDataAdapter adapt;
public DB_class()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection GetConnection()
{
string mystr = ConfigurationManager.AppSettings["myconnect"].ToString();
SqlConnection myconn = new SqlConnection(mystr);
return myconn;
}
public int ExecNonQuery(string strSql)
{
try
{
myconn = GetConnection();//与数据库连接
mycmd = new SqlCommand();//初始化SqlCommand类对象
mycmd.Connection = myconn;
mycmd.CommandText = strSql;
if (mycmd.Connection.State != ConnectionState.Open)
{
mycmd.Connection.Open();//打开与数据库的连接
}
int a=mycmd.ExecuteNonQuery();//执行Sql操作,并返回受影响的行数
return a;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (mycmd.Connection.State == ConnectionState.Open)
{//断开连接,释放资源
mycmd.Connection.Close();
myconn.Dispose();
mycmd.Dispose();
}
}
}
public string ExecScalar(string strSql)
{
try
{
myconn = GetConnection();//与数据库连接
mycmd = new SqlCommand();//初始化SqlCommand类对象
mycmd.Connection = myconn;
mycmd.CommandText = strSql;
if (mycmd.Connection.State != ConnectionState.Open)
{
mycmd.Connection.Open();//打开与数据库的连接
}
//使用SqlCommand对象的ExecuteScalar方法返回第一行第一列的值
//strSql = Convert.ToString(mycmd.ExecuteScalar());
string another = Convert.ToString(mycmd.ExecuteScalar());
return another;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
if (mycmd.Connection.State == ConnectionState.Open)
{//断开连接,释放资源
myconn.Dispose();
mycmd.Connection.Close();
mycmd.Dispose();
}
}
}
public DataTable GetDataSet(string strSql, string TableName)
{
ds = new DataSet();
try
{
myconn = GetConnection();//与数据库连接
adapt = new SqlDataAdapter(strSql, myconn); //实例化SqlDataAdapter类对象
adapt.Fill(ds, TableName);//填充数据集
return ds.Tables[TableName];//返回数据集DataSet的表的集合
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{//断开连接,释放资源
myconn.Close();
adapt.Dispose();
ds.Dispose();
myconn.Dispose();
}
}
}
.net/c#连接sqlserver的更多相关文章
- php以pdo方式连接sqlserver,无法开启sqlsrv扩展
首先,PHP连接sqlserver代码如下: $host="aaaaa"; $dbname="aaaaa"; $user="aaaaa"; ...
- 用Window Authentication的方式去连接SQLServer
用Window Authentication的方式去连接SQLServer Connection String: jdbc:sqlserver://${serverName};databaseName ...
- Android通过webservice连接SQLServer 详细教程(数据库+服务器+客户端)
http://blog.csdn.net/zhyl8157121/article/details/8169172 目录(?)[-] 项目说明 开发环境的部署 数据库设计 服务器端程序设计Webserv ...
- ORACLE连接SQLSERVER
一.实验(实验成功) 1.实验目标:ORACLE连接SQLSERVER以及查询数据 2.搭建的环境: oracle 9i 9.0.2.0.1 地址:192.168.40.139 sql2000 的数据 ...
- Jfinal连接SQLSERVER相关配置说明
driver=net.sourceforge.jtds.jdbc.Driver jdbcUrl = jdbc:sqlserver://localhost:1433;DatabaseName=test ...
- jsp连接sqlServer数据库教程
一.首先讲下我用的工具版本以供参考: jar包:jtds1.3.1.jar 下载地址:点击进入 数据库:SQL Server2012 服务器:Tomcat8.0 下载地址:点击进入 开发IDE工 ...
- navicat 连接sqlserver提示要安装 sql server native client
navicat 连接sqlserver提示要安装 sql server native client 解决方法:其实navicat自带sqlncli_x64.msi,就在安装目录下,安装后问题解决!
- 使用thinkphp连接sqlserver数据库时提示“系统不支持:sqlsrv”
习惯了使用php跟mysql组合,现在接到项目需要调用客户线下的系统软件的数据,具了解,这个软件的数据库是用sqlserver数据库也就是常说的mssql数据库了. 那么我现在需要用PHP连接sqls ...
- PHP:PHP5.4连接SQLSERVER
在PHP5.4的环境下要连接SQLSERVER(2005/2008)数据库,需要以下步骤: 1.下载PHP5.4连接SQLSERVER的驱动(两个动态连接库)http://www.microsoft. ...
- PHP连接sqlserver的两种方法,向sqlserver2000中写入数据,中文乱码
项目环境是php5.3.28 项目用的ThinkPHP3.2.3 已经mysql5.5数据库,要和另一个项目对接,需要连接sqlsever2000数据库进行一些操作. 第一种用php自带扩展连接数据 ...
随机推荐
- 用任务管理器画CPU正弦曲线
这个最初是在microsoft的<编程之美>中看到的,用你的程序来控制CPU的使用率. 首先是要求写一个用来实现CPU使用率为50%程序. 这个还是很好实现的,只要让你的程序忙的时间课空闲 ...
- linux scp命令参数及用法详解--linux远程复制拷贝命令使用实例【转】
转自:http://blog.csdn.net/jiangkai_nju/article/details/7338177 一般情况,本地网络跟远程网络进行数据交抱,或者数据迁移,常用的有三种方法,一是 ...
- itnesse实现api接口自动化测试学习
上午在园子里乱逛,看了不少小伙伴们分享的接口测试方面的知识,大家所叙述到的一些经验或多或少,我也曾遇到过,突然意识到知识的点滴积累是多么的重要,我记得我最早接触接口测试的时候,就是只在浏览器里人工测试 ...
- bigtint;int;smallint;tinyint
bigint对应的是Int64 [long] int对应的是Int32 [int] smallint对应的是Int16 [short] tinyint对应的是 [byte ...
- 第一篇 ERP是什么?-从道的层面浅谈我的理解
世界上称为ERP软件的软件很多,国外的有SAP,ORACLE,国内的有金蝶,用友,浪潮.这些由不同的厂商开发制作的软件总有其软件适用的场合.这个场合就是企业,而且是市场经济中的企业.个人是不会购买ER ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Codeforces Round #320 (Div. 2) D. "Or" Game 数学
D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- POJ 3107 Godfather (树形dp)
题目链接 虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久. 还有据说这个题用vector会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...
- Android中GridView拖拽的效果【android进化三十六】
最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的 ...
- JAVA使用JNI调用C++动态链接库
JAVA使用JNI调用C++动态链接库 使用JNI连接DLL动态链接库,并调用其中的函数 首先 C++中写好相关函数,文件名为test.cpp,使用g++编译为DLL文件,指令如下: g++ -sha ...