C#怎么使用DBHelper类连接数据库?

C#怎么使用DBHelper类连接数据库?,第1张

首先你得有DBHelpler这个类,然后你看这个类中你要进行调用的方法是不是静态的如果是静态的直接用类名.方法名即可,前提是引用这个类的命名空间,如果不是静态的话,那么就new一下这个类,然后用这个new的DBHelper的变量点出你要用的方法即可,如果你没有DBHelper的话,你可以去下载一个,或者找一个,或者自己写一个。

using System

using System.Collections.Generic

using System.Linq

using System.Text

using System.Data

using System.Data.SQLite

namespace DAL

{

static class SqlHelper

{

private static bool Conn = false

private static string ConnStr = @"Data Source=FileNamePooling=trueFailIfMissing=false"

static SqlHelper()

{

Conn = TestConn()

}

public static bool Update(string SQL, params SqlParameter[] SqlParameters)

{

bool Result = false

if (Conn)

using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr))

{

SqlConn.Open()

using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn))

{

if (SQLiteParameters != null)

SqlCmd.Parameters.AddRange(SqlParameters)

Result = SQLiteCmd.ExecuteNonQuery() >0

}

}

return Result

}

public static T Scalar<T>(string SQL, params SQLiteParameter[] SqlParameters)

{

object Result = null

if (Conn)

using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr))

{

SqlConn.Open()

using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn))

{

if (SQLiteParameters != null)

SqlCmd.Parameters.AddRange(SqlParameters)

Result = SqlCmd.ExecuteScalar()

}

}

return (Result != null) ? (T)Result : default(T)

}

public static DataTable Query(string SQL, params SQLiteParameter[] SqlParameters)

{

DataTable Result = null

if (Conn)

using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr))

{

SqlConn.Open()

using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn))

{

if (SqlParameters != null)

SqlCmd.Parameters.AddRange(SqlParameters)

using (SQLiteDataAdapter SqlAdap = new SQLiteDataAdapter(SqlCmd))

using (DataSet ds = new DataSet())

{

SqlAdap.Fill(ds)

Result = ds.Tables[0]

}

}

}

return Result

}

private static bool TestConn()

{

bool TestResult = false

try

{

using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr))

SqlConn.Open()

TestResult = true

}

catch

{

TestResult = false

}

return TestResult

}

}

}

using Systemusing System.Collections.Genericusing System.Linqusing System.Textusing System.Datausing System.Data.SQLitenamespace DAL { static class SqlHelper { private static bool Conn = falseprivate static string ConnStr = @"Data Source=FileNamePooling=trueFailIfMissing=false"static SqlHelper() { Conn = TestConn()} public static bool Update(string SQL, params SqlParameter[] SqlParameters) { bool Result = falseif (Conn) using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr)) { SqlConn.Open()using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn)) { if (SQLiteParameters != null) SqlCmd.Parameters.AddRange(SqlParameters)Result = SQLiteCmd.ExecuteNonQuery() >0} } return Result} public static T Scalar<T>(string SQL, params SQLiteParameter[] SqlParameters) { object Result = nullif (Conn) using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr)) { SqlConn.Open()using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn)) { if (SQLiteParameters != null) SqlCmd.Parameters.AddRange(SqlParameters)Result = SqlCmd.ExecuteScalar()} } return (Result != null) ? (T)Result : default(T)} public static DataTable Query(string SQL, params SQLiteParameter[] SqlParameters) { DataTable Result = nullif (Conn) using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr)) { SqlConn.Open()using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn)) { if (SqlParameters != null) SqlCmd.Parameters.AddRange(SqlParameters)using (SQLiteDataAdapter SqlAdap = new SQLiteDataAdapter(SqlCmd)) using (DataSet ds = new DataSet()) { SqlAdap.Fill(ds)Result = ds.Tables[0]} } } return Result} private static bool TestConn() { bool TestResult = falsetry { using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr)) SqlConn.Open()TestResult = true} catch { TestResult = false} return TestResult} } }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6762966.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存