第一个项目总结
基类:database:主要是定义有关数据库的方法:
1.打开数据库
public static void Open()
{ ( "server=.\sqlexpress;database=Shengying;Intergrated Security=ture")
SqlConnection conn=new SqlConnection("server=.\sqlexpress;database= SheYing;uid=sa;pwd=123456");
conn.OPen();
}
2.关闭数据库
public static void Close()
{
if(conn!=null)
{
conn.close();
conn.dispose();释放资源
}
}
3.打开数据库后,通过查询sql语句,或得数据,将数据放到DataTable表中,
public static DataTable GetDt(string strsql)
{
DataSet ds=new DataSet();DataSet是DataTable的集合
ds=GetDs(strsql);
if(ds.Tables.Count>0)
{
return ds.Table[0];
}
else
{
return null;
}
}
3.1 打开数据库后,通过查询sql语句,将数据放到DataSet表中
public static DataSet GetDs (string strsql)
{
DataSet ds=new DataSet();
Open();
SqlDataAdapte da=new SqlDataAdapte(strsql,conn);//通过适配器获取数据:1.sql语句2.打开数据库
da.Fill(ds);//将适配器获取数据获取的数据填充到DataSet;
Close();
return ds;//要返回ds里存储的数据
}
4.通过SQL语句返回SqlDataReader数据流///要调用SqlCommand
public static SqlDataReader GetSDR(string strsql)
{
Open();
SqlCommand com = new SqlCommand(strsql, conn);
sdr=com.ExecuteReader(system.Data.CommandBehavior.CloseConnection);
return sdr;s
}
5.通过SQL语句返回DataView??????????????????????????????
public static DataView GetDV(string strsql)
{
ds = new DataSet();
ds = GetDs(strsql);
if (ds.Tables.Count > 0)
{
return ds.Tables[0].DefaultView;
}
else
{
return null;
}
}
6.执行sql语句,看看是否执行sql语句执行成功,用到SqlCommand的调用ExecuteNonQuery()
public static bool BoolRunSQl(string sql)
{
OPen();
try
{
cmd=new SqlCommand(strsql,conn);
cmd.ExecuteNonQuery();
close();
retyrn ture;
}
else
{
return false;
}
}
7.执行一条SQL语句,返回所影响的行数
public static int intRunSQl(string strsql)
{
int strRun=0;
Open();
try
{
com=new SqlCommand(strsql,conn);
strRun=com.ExecuteNonQuery();
Close();
return strRun;
}
catch
{
return strRun;
}
}
8.返回第一行第一列的数据:comm.ExecuteScalar()
public static string strRunSQL(string strsql)
{
string strRun="";
Open();
try
{
com=new SqlCommand(strsql,conn);
strRun=com.ExecuteScalar().ToString();
Close();
return strRun;
}
catch
{
retyrn "";
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
/// <summary>
/// database 的摘要说明
/// </summary>
public class database
{
//声明6个
private static SqlConnection conn;
private static SqlDataAdapter da;//适配器
private static DataSet ds;//
private static DataTable dt;
private static SqlCommand comm;
private static SqlDataReader sdr;
public database()
{
//这是一个所有方法的类,打开(关闭)数据库
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 打开数据库连接
/// </summary>
private static void OPen()
{
//conn = new SqlConnection("server=.\sqlexpress;database=ShenYing;integrated security=true");
conn = new SqlConnection("server=.\sqlexpress;database= SheYing;uid=sa;pwd=123456");
conn.Open();
}
/// <summary>
/// 关闭数据库连接
/// </summary>
private static void Close()
{
if (conn != null)
{
//关闭
conn.Close();
conn.Dispose();//释放资源
}
}
/// <summary>
/// 通过SQL语句返回DataSet
/// </summary>
/// <param name="strsql">sql查询语句string</param>
/// <returns></returns>
private static DataSet GetDs(string strsql)
{
OPen();
da=new SqlDataAdapter (strsql,conn);
ds=new DataSet();
da.Fill(ds);
Close();
return ds;
}
/// <summary>
/// 通过SQL语句返回DataTable
/// </summary>
/// <param name="strsql">SQL查询语句string</param>
/// <returns></returns>
public static DataTable GetDt(string strsql)//通过生气了语句获取数据,放到DataTable里,然后放到DataSet中
{
ds = new DataSet();
ds = GetDs(strsql);
if (ds.Tables.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
/// <summary>
/// 通过SQL语句返回SqlDataReader
/// </summary>
/// <param name="sdr">SQL语句strin</param>
/// <returns></returns>
public static SqlDataReader GetSDR(string strsql)
{
OPen();
comm = new SqlCommand(strsql, conn);
sdr = comm.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return sdr;
}
/// <summary>
/// 通过SQL语句返回DataView
/// </summary>
/// <param name="strsql">SQL语句string</param>
/// <returns></returns>
public static DataView GetDV(string strsql)
{
ds = new DataSet();
ds = GetDs(strsql);
if (ds.Tables.Count > 0)
{
return ds.Tables[0].DefaultView;
}
else
{
return null;
}
}
/// <summary>
/// 执行SQL语句,返回是否执行成功bool
/// </summary>
/// <param name="strsql">SQL语句</param>
/// <returns></returns>
public static bool boolRunSQL(string strsql)
{
OPen();
try
{
//无错误返回True
comm=new SqlCommand(strsql,conn);
comm.ExecuteNonQuery();
Close();
return true;
}
catch (Exception)
{
//有错误时返回false
return false;
}
}
/// <summary>
/// 执行一条SQL语句,返回所影响的行数
/// </summary>
/// <param name="strsql"></param>
public static int intRunSQL(string strsql)
{
int strRun =0;
OPen();
try
{
comm = new SqlCommand(strsql, conn);
strRun = comm.ExecuteNonQuery();
Close();
return strRun;
}
catch
{
return strRun;
}
}
/// <summary>
/// 返回第一行第一列的数据
/// </summary>
/// <param name="strsql"></param>
/// <returns></returns>
public static string strRunSQL(string strsql)
{
string strRun = "";
OPen();
try
{
comm = new SqlCommand(strsql, conn);
strRun=comm.ExecuteScalar().ToString();
Close();
return strRun;
}
catch
{
return "";
}
}
}