sql 的幾種常用方法
第一個項目總結
基類: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 "";
}
}
}
sql 的幾種常用方法