Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL和SQLite的包装器_C#_Sql_Sqlite - Fatal编程技术网

C# SQL和SQLite的包装器

C# SQL和SQLite的包装器,c#,sql,sqlite,C#,Sql,Sqlite,我正在尝试扩展一个最初使用sqlite数据库设计的应用程序,以同时与sql server接口。我最初的想法是创建一个包装器类来处理这些事务。使用Sqlselect函数返回数据,使用SQLexecute函数执行非查询事务 我最初的尝试是这样的: public static class Sqlwrapper { public static int Sqltype = 0; /// <summary> /// Fills a datatable with the

我正在尝试扩展一个最初使用sqlite数据库设计的应用程序,以同时与sql server接口。我最初的想法是创建一个包装器类来处理这些事务。使用Sqlselect函数返回数据,使用SQLexecute函数执行非查询事务

我最初的尝试是这样的:

public static class Sqlwrapper
{

    public static int Sqltype = 0;

    /// <summary>
    /// Fills a datatable with the result of the select command.
    /// </summary>
    /// <param name="cmdstring"> Connection string </param>
    /// <param name="dt">Datatable to be filled</param>
    /// <returns>Returns true if successful, false if there is any issues.</returns>
    public static bool Sqlselect(string cmdstring, out DataTable dt)
    {
        dt = new DataTable();

        if (Sqltype == 0)
        {
            SQLiteConnection c = new SQLiteConnection("Data Source=Resources\\DB.sqlite;Version=3");
            try
            {
                c.Open();
                SQLiteDataAdapter a = new SQLiteDataAdapter(cmdstring, c);
                a.Fill(dt);
                c.Close();
                return true;
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
                c.Close();
                return false;
            }
        }
        else
        {
            SqlConnection c = new SqlConnection("Server=Server;Database=DB;Trusted_Connection=True;");
            try
            {
                c.Open();
                SqlDataAdapter a = new SqlDataAdapter(cmdstring, c);
                a.Fill(dt);
                c.Close();
                return true;
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
                c.Close();
                return false;
            }
        }
    }

    /// <summary>
    /// Executes a sql command
    /// </summary>
    /// <param name="cmdstring">sql command string</param>
    /// <returns>True for success.</returns>
    public static bool Sqlexecute(string cmdstring)
    {
        if (Sqltype == 0)
        {
            SQLiteConnection c = new SQLiteConnection("Data Source=Resources\\DB.sqlite;Version=3");
            try
            {
                c.Open();
                SQLiteCommand cmd = new SQLiteCommand(cmdstring, c);
                cmd.ExecuteNonQuery();
                c.Close();
                return true;
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
                c.Close();
                return false;
            }
        }
        else
        {
            SqlConnection c = new SqlConnection("Server=Server;Database=DB;Trusted_Connection=True;");
            try
            {
                c.Open();
                SqlCommand cmd = new SqlCommand(cmdstring, c);
                cmd.ExecuteNonQuery();
                c.Close();
                return true;
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
                c.Close();
                return false;
            }
        }
    }

}
公共静态类Sqlwrapper
{
公共静态int Sqltype=0;
/// 
///使用select命令的结果填充数据表。
/// 
///连接字符串
///要填写的数据表
///如果成功,则返回true;如果存在任何问题,则返回false。
公共静态bool Sqlselect(string cmdstring,out DataTable dt)
{
dt=新数据表();
if(Sqltype==0)
{
SQLiteConnection c=newsqliteconnection(“数据源=Resources\\DB.sqlite;版本=3”);
尝试
{
c、 Open();
SQLiteDataAdapter a=新的SQLiteDataAdapter(cmdstring,c);
a、 填充(dt);
c、 Close();
返回true;
}
捕获(异常exc)
{
Debug.WriteLine(exc.Message);
c、 Close();
返回false;
}
}
其他的
{
SqlConnection c=newsqlconnection(“Server=Server;Database=DB;Trusted_Connection=True;”);
尝试
{
c、 Open();
SqlDataAdapter a=新的SqlDataAdapter(cmdstring,c);
a、 填充(dt);
c、 Close();
返回true;
}
捕获(异常exc)
{
Debug.WriteLine(exc.Message);
c、 Close();
返回false;
}
}
}
/// 
///执行sql命令
/// 
///sql命令字符串
///对成功来说是正确的。
公共静态bool Sqlexecute(字符串cmdstring)
{
if(Sqltype==0)
{
SQLiteConnection c=newsqliteconnection(“数据源=Resources\\DB.sqlite;版本=3”);
尝试
{
c、 Open();
SQLiteCommand cmd=新的SQLiteCommand(cmdstring,c);
cmd.ExecuteNonQuery();
c、 Close();
返回true;
}
捕获(异常exc)
{
Debug.WriteLine(exc.Message);
c、 Close();
返回false;
}
}
其他的
{
SqlConnection c=newsqlconnection(“Server=Server;Database=DB;Trusted_Connection=True;”);
尝试
{
c、 Open();
SqlCommand cmd=新的SqlCommand(cmdstring,c);
cmd.ExecuteNonQuery();
c、 Close();
返回true;
}
捕获(异常exc)
{
Debug.WriteLine(exc.Message);
c、 Close();
返回false;
}
}
}
}
这似乎有效,但我遇到了一些问题,例如。从sqlserver开始,我需要添加一个用户名,对于本地数据库,我们不发送该数据

我传递命令字符串和/或获取数据表的方法是一种好的做法吗?有没有更简单的方法我忽略了

谢谢

编辑: 下面是第二个实现:

public class DbManager
{
    public IDbProvider DbProvider;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sqltype">An integer to set which database type to use, defaults to 0 for sqlite, set to 1 for sql server.</param>
    public DbManager(int sqltype = 0)
    {
        if (sqltype == 0)
        {
            DbProvider = new SqliteWrapper();
        }
        else
        {
            DbProvider = new SqlWrapper();
        }
    }
}

public interface IDbProvider
{
    /// <summary>
    /// Fills a datatable with the result of the select command.
    /// </summary>
    /// <param name="cmdstring"> Connection string </param>
    /// <param name="dt">Datatable to be filled</param>
    /// <returns>Returns true if successful, false if there is any issues.</returns>
    bool Sqlselect(string cmdstring, out DataTable dt);

    /// <summary>
    /// Executes a sql command
    /// </summary>
    /// <param name="cmdstring">sql command string</param>
    /// <returns>True for success.</returns>
    bool Sqlexecute(string cmdstring);
}

public class SqliteWrapper : IDbProvider
{
    /// <summary>
    /// Fills a datatable with the result of the select command.
    /// </summary>
    /// <param name="cmdstring"> Connection string </param>
    /// <param name="dt">Datatable to be filled</param>
    /// <returns>Returns true if successful, false if there is any issues.</returns>
    public bool Sqlselect(string cmdstring, out DataTable dt)
    {
        dt = new DataTable();
        SQLiteConnection c = new SQLiteConnection("Data Source=Resources\\DB.sqlite;Version=3");
        try
        {
            c.Open();
            SQLiteDataAdapter a = new SQLiteDataAdapter(cmdstring, c);
            a.Fill(dt);
            c.Close();
            return true;
        }
        catch (Exception exc)
        {
            Debug.WriteLine(exc.Message);
            c.Close();
            return false;
        }
    }

    /// <summary>
    /// Executes a sql command
    /// </summary>
    /// <param name="cmdstring">sql command string</param>
    /// <returns>True for success.</returns>
    public bool Sqlexecute(string cmdstring)
    {
        SQLiteConnection c = new SQLiteConnection("Data Source=Resources\\DB.sqlite;Version=3");
        try
        {
            c.Open();
            SQLiteCommand cmd = new SQLiteCommand(cmdstring, c);
            cmd.ExecuteNonQuery();
            c.Close();
            return true;
        }
        catch (Exception exc)
        {
            Debug.WriteLine(exc.Message);
            c.Close();
            return false;
        }
    }
}

public class SqlWrapper : IDbProvider
{
    /// <summary>
    /// Fills a datatable with the result of the select command.
    /// </summary>
    /// <param name="cmdstring"> Connection string </param>
    /// <param name="dt">Datatable to be filled</param>
    /// <returns>Returns true if successful, false if there is any issues.</returns>
    public bool Sqlselect(string cmdstring, out DataTable dt)
    {
        dt = new DataTable();

        SqlConnection c = new SqlConnection("Server=Server;Database=DB;Trusted_Connection=True;");
        try
        {
            c.Open();
            SqlDataAdapter a = new SqlDataAdapter(cmdstring, c);
            a.Fill(dt);
            c.Close();
            return true;
        }
        catch (Exception exc)
        {
            Debug.WriteLine(exc.Message);
            c.Close();
            return false;
        }

    }
    public bool Sqlexecute(string cmdstring)
    {


        SqlConnection c = new SqlConnection("Server=Server;Database=DB;Trusted_Connection=True;");
        try
        {
            c.Open();
            SqlCommand cmd = new SqlCommand(cmdstring, c);
            cmd.ExecuteNonQuery();
            c.Close();
            return true;
        }
        catch (Exception exc)
        {
            Debug.WriteLine(exc.Message);
            c.Close();
            return false;
        }

    }
}
公共类DbManager
{
公共服务提供者;
/// 
/// 
/// 
///一个整数,用于设置要使用的数据库类型,sqlite的默认值为0,sql server的默认值为1。
公共DbManager(int-sqltype=0)
{
if(sqltype==0)
{
DbProvider=新的SqliteWrapper();
}
其他的
{
DbProvider=newsqlwrapper();
}
}
}
公共接口IDB提供程序
{
/// 
///使用select命令的结果填充数据表。
/// 
///连接字符串
///要填写的数据表
///如果成功,则返回true;如果存在任何问题,则返回false。
bool-Sqlselect(string-cmdstring,out-DataTable-dt);
/// 
///执行sql命令
/// 
///sql命令字符串
///对成功来说是正确的。
bool-Sqlexecute(字符串cmdstring);
}
公共类SqliteWrapper:IDbProvider
{
/// 
///使用select命令的结果填充数据表。
/// 
///连接字符串
///要填写的数据表
///如果成功,则返回true;如果存在任何问题,则返回false。
public bool Sqlselect(string cmdstring,out DataTable dt)
{
dt=新数据表();
SQLiteConnection c=newsqliteconnection(“数据源=Resources\\DB.sqlite;版本=3”);
尝试
{
c、 Open();
SQLiteDataAdapter a=新的SQLiteDataAdapter(cmdstring,c);
a、 填充(dt);
c、 Close();
返回true;
}
捕获(异常exc)
{
Debug.WriteLine(exc.Message);
c、 Close();
返回false;
}
}
/// 
///执行sql命令
/// 
///sql命令字符串
///对成功来说是正确的。
公共bool Sqlexecute(字符串cmdstring)
{
SQLiteConnection c=newsqliteconnection(“数据源=Resources\\DB.sqlite;版本=3”);
尝试
{
c、 Open();
SQLiteCommand cmd=新的SQLiteCommand(cmdstring,c);
cmd.ExecuteNonQuery();
c、 Close();
返回true;
}
捕获(异常exc)
{
Debug.WriteLine(exc.Message);
c、 Close();
返回false;
}
}
}
公共类SqlWrapper:IDbProvider
{
/// 
///使用select命令的结果填充数据表。
/// 
///连接字符串
///要填写的数据表
///如果成功,则返回true;如果存在任何问题,则返回false。
public abstract class SqlFactory
{
    public abstract DbConnection CreateConnection();

    public abstract DataAdapter CreateAdapter(string command, DbConnection connection);

}

public class SqlLiteFactory : SqlFactory
{
    public override DbConnection CreateConnection()
    {
        return new SQLiteConnection("Data Source=Resources\\DB.sqlite;Version=3");
    }

    public override DataAdapter CreateAdapter(string command, DbConnection connection)
    {
        return new SQLiteDataAdapter(command, connection as SQLiteConnection);
    }
}

public class MSSqlFactory : SqlFactory
{
    public override DbConnection CreateConnection()
    {
        return new SqlConnection("CONNECTION STRING HERE");
    }

    public override DataAdapter CreateAdapter(string command, DbConnection connection)
    {
        return new SqlDataAdapter(command, connection as SqlConnection);
    }
}

//Composite and Singleton class...
public class SqlHandler : SqlFactory
{
    private static SqlHandler _instance;

    private SqlLiteFactory _sqlLiteFactory;
    private MSSqlFactory _msSqlFactory;

    //Singleton pattern.
    public static SqlHandler Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new SqlHandler();
            }

            return _instance;
        }
    }

    private SqlHandler()
    {
        _sqlLiteFactory = new SqlLiteFactory();
        _msSqlFactory = new MSSqlFactory();
    }

    public override DbConnection CreateConnection()
    {
        //Some code determining if better to use SqlLite or MS SQL.
        if (useSqlLite)
        {
            return _sqlLiteFactory.CreateConnection();
        }
        else
        {
            return _msSqlFactory.CreateConnection();
        }
    }

    public override DataAdapter CreateAdapter(string command, DbConnection connection)
    {
        //Some code determining if better to use SqlLite or MS SQL.
        if (useSqlLite)
        {
            return _sqlLiteFactory.CreateAdapter(command, connection);
        }
        else
        {
            return _msSqlFactory.CreateAdapter(command, connection);
        }
    }
}
DbConnection c = SqlHandler.Instance.CreateConnection();