C# Windows froms 窗体程序,,连接SQLite数据库的,DBhelp类怎写
怎么把这段代码封装到DBHELP类里面,,我调用一直不可以用stringdatasource="f:/sqlitesss.db";if(!System.IO.File.E...
怎么把这段代码封装到DBHELP类里面,,我调用一直不可以用 string datasource = "f:/sqlitesss.db"; if (!System.IO.File.Exists(datasource))//判断数据库是否存在
SQLiteConnection.CreateFile(datasource);//创建数据库文件
SQLiteConnection conn = new SQLiteConnection();
SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
connstr.DataSource = datasource;
conn.ConnectionString = connstr.ToString();
conn.Open(); 展开
SQLiteConnection.CreateFile(datasource);//创建数据库文件
SQLiteConnection conn = new SQLiteConnection();
SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
connstr.DataSource = datasource;
conn.ConnectionString = connstr.ToString();
conn.Open(); 展开
2013-06-10
展开全部
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=FileName;Pooling=true;FailIfMissing=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 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=FileName;Pooling=true;FailIfMissing=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;
}
}
}
2013-06-10
展开全部
3906077
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-10
展开全部
你的datasource有问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询