using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DB
{
//DBHelper实例模板
class DBHelper
{
//连接数据库字符串
private const string sqlStr = @"";
//返回第一行 第一列
public object SelecOne(string sql)
{
object obj = null;
SqlConnection conn = new SqlConnection(sqlStr);
try
{
//打开数据库连接
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
obj= cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//关闭数据库连接
conn.Close();
}
return obj;
}
//执行update
public int Update(string sql)
{
int obj = -1;
SqlConnection conn = new SqlConnection(sqlStr);
try
{
//打开数据库连接
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
obj=cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//关闭数据库连接
conn.Close();
}
return obj;
}
//执行读取多行 多列dataReader
public SqlDataReader SelectAll(string sql)
{
SqlDataReader dr = null;
SqlConnection conn = new SqlConnection(sqlStr);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return dr;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DB
{
//DBHelper实例模板
class DBHelper
{
//连接数据库字符串
private const string sqlStr = @"";
//返回第一行 第一列
public object SelecOne(string sql)
{
object obj = null;
SqlConnection conn = new SqlConnection(sqlStr);
try
{
//打开数据库连接
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
obj= cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//关闭数据库连接
conn.Close();
}
return obj;
}
//执行update
public int Update(string sql)
{
int obj = -1;
SqlConnection conn = new SqlConnection(sqlStr);
try
{
//打开数据库连接
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
obj=cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//关闭数据库连接
conn.Close();
}
return obj;
}
//执行读取多行 多列dataReader
public SqlDataReader SelectAll(string sql)
{
SqlDataReader dr = null;
SqlConnection conn = new SqlConnection(sqlStr);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return dr;
}
}
}