?? sql.cs
字號(hào):
+using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
//數(shù)據(jù)庫(kù)操作類(lèi)
public class Sql
{
private string str;//數(shù)據(jù)庫(kù)連接字符串
public SqlConnection Con;//數(shù)據(jù)庫(kù)連接
public Sql()
{
str = GetConn();//得到數(shù)據(jù)庫(kù)連接字符串
Con = new SqlConnection(str);//數(shù)據(jù)庫(kù)連接對(duì)象
}
public static string GetConn()
{
//數(shù)據(jù)庫(kù)的路徑
string path = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath);
//數(shù)據(jù)庫(kù)連接字符串
return "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"" + path + "\\App_Data\\tv.mdf\";Integrated Security=True;User Instance=True";
}
// 輸入查詢字符串,返回dataset
public DataSet GetDS(string sql)
{
SqlCommand command = new SqlCommand(sql, Con);//初始化命令對(duì)象
command.CommandText = sql;//賦予語(yǔ)句
DataSet dt = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);//適配器
Con.Open();
da.Fill(dt);//獲得數(shù)據(jù)
command.Connection.Close();//關(guān)閉連接
return dt;
}
// 是否關(guān)閉數(shù)據(jù)庫(kù)連接 可以選擇;sql:語(yǔ)句;closeConnection:是否關(guān)閉連接
public void ExecuteSql(string sql, bool closeConnection)
{
if (Con.State != ConnectionState.Open)//如果連接未打開(kāi),則打開(kāi)連接
Con.Open();
//命令
SqlCommand Com = new SqlCommand(sql, Con);
Com.ExecuteNonQuery();//執(zhí)行
if (closeConnection) Con.Close();//如果需要關(guān)閉,則關(guān)閉
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -