You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMDatabaseSample.Util
{
internal sealed class SqlDBManager
{
public static SqlDBManager Instance { get; private set; } = new SqlDBManager();
public string ConnectionString { get; set; }
public SqlConnection Connection { get; private set; }
private SqlCommand _sqlCommand;
public bool GetConnection()
{
try
{
_sqlCommand = new SqlCommand();
_sqlCommand.Connection = this.Connection;
this.Connection = new SqlConnection(ConnectionString);
this.Connection.Open();
return true;
}
catch (Exception)
{
return false;
}
}
public DataSet ExecuteQuery(DataSet ds, string query)
{
ds.Reset();
lock (this)
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = _sqlCommand;
adapter.SelectCommand.Connection = this.Connection;
adapter.SelectCommand.CommandText = query;
adapter.Fill(ds);
}
return ds;
}
}
}