c#连接sqlserver数据库
2014-08-07 16:20
260 查看
首先需要导入的一些
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
namespace WebApplication5
{
public class DBHelper
{
private static string dbIP = "127.0.0.1";
private static string dbName = "myTest";
private static string dbUserName = "yang";
private static string dbUserPwd = "yang";
private SqlConnection conn;
//获取数据库连接
public SqlConnection getConn() {
string strconn = @"server="+dbIP+";database="+dbName+";uid="+dbUserName+";pwd="+dbUserPwd+"";
conn = new SqlConnection(strconn);
try
{
conn.Open();
}
catch (System.Exception ex)
{
Console.WriteLine("[ERROR] 数据库操作出现异常:" + ex.Message);
}
return conn;
}
//查询操作
public SqlDataReader search(string str)
{
conn = getConn();
SqlCommand myCommand = new SqlCommand(str,conn);
SqlDataReader rd = myCommand.ExecuteReader();
return rd;
}
//增删改数据
public int addOrDelOrUpdata(string str) {
int result = 0;
conn = getConn();
string strConn = @"" + str;
SqlCommand myCommand = new SqlCommand(strConn, conn);
try
{
result = myCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.",ex);
}
return result;
}
//关闭sqldatareader连接
public void closeSqlDataReader(SqlDataReader rd) {
if (rd != null) {
rd.Close();
}
}
//关闭sqlconnection连接
public void closeConn(SqlConnection conn) {
if (conn != null) {
conn.Close();
}
}
}
}
实际使用中直接调用改DBHelper的方法,传入需要的sql语句
select * from tbTree
update tbTree set Context = 'xxxx' where id = 1;
insert into tbTree values('苏州',19);
delete from tbTree where id = 16;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
namespace WebApplication5
{
public class DBHelper
{
private static string dbIP = "127.0.0.1";
private static string dbName = "myTest";
private static string dbUserName = "yang";
private static string dbUserPwd = "yang";
private SqlConnection conn;
//获取数据库连接
public SqlConnection getConn() {
string strconn = @"server="+dbIP+";database="+dbName+";uid="+dbUserName+";pwd="+dbUserPwd+"";
conn = new SqlConnection(strconn);
try
{
conn.Open();
}
catch (System.Exception ex)
{
Console.WriteLine("[ERROR] 数据库操作出现异常:" + ex.Message);
}
return conn;
}
//查询操作
public SqlDataReader search(string str)
{
conn = getConn();
SqlCommand myCommand = new SqlCommand(str,conn);
SqlDataReader rd = myCommand.ExecuteReader();
return rd;
}
//增删改数据
public int addOrDelOrUpdata(string str) {
int result = 0;
conn = getConn();
string strConn = @"" + str;
SqlCommand myCommand = new SqlCommand(strConn, conn);
try
{
result = myCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.",ex);
}
return result;
}
//关闭sqldatareader连接
public void closeSqlDataReader(SqlDataReader rd) {
if (rd != null) {
rd.Close();
}
}
//关闭sqlconnection连接
public void closeConn(SqlConnection conn) {
if (conn != null) {
conn.Close();
}
}
}
}
实际使用中直接调用改DBHelper的方法,传入需要的sql语句
select * from tbTree
update tbTree set Context = 'xxxx' where id = 1;
insert into tbTree values('苏州',19);
delete from tbTree where id = 16;
相关文章推荐
- C#连接sqlserver数据库
- c#连接sqlserver数据库
- c#连接sqlserver数据库并显示
- C#连接SQLServer数据库
- c# 连接sqlserver数据库
- 用ASP.NET/C#连接Access和SQLServer数据库
- c#连接Sqlserver数据库
- C# 连接SQLSERVER数据库 连接字符串
- C# 连接、操作SqlServer数据库--日常笔记
- c#连接sqlserver数据库
- c#连接sqlserver数据库
- c#连接sqlserver数据库
- c#sqlserver数据库连接字符串
- c#连接sqlserver数据库、插入数据、从数据库获取时间示例
- C#(ASP.NET)中连接数据库的字符串(连接Access和sqlserver数据库以及用windows和身份验证两种方式)
- c#连接sqlserver数据库
- C# 连接SQLServer数据库及登录验证知识
- C#与SQLServer数据库连接
- C#连接sqlServer数据库详解
- C#连接Access数据库,C#连接Excel数据库,C#连接SqlServer数据库,C#连接Mysql数据库总结