我想更新数据网格数据以访问数据库,为数据库连接创建了单独的表,我想从更新按钮表单中的数据库类调用以更新访问数据库。
请在下面找到数据库连接代码和updatebutton表单代码我不知道如何在updatebutton表单中调用DBIO类方法
更新按钮表单代码
namespace CatiaLicenseUsageReport
{
public partial class FrmCatiaLicenseUsageReport : Form
{
FileIO objFileIO ;
Utilities objUtilities ;
DBIO objDBIO ;
private string logfilepath = "";
private string DBPath = "";
[This how i created the class for DBIO class][1]private void Initialize()
{
string logfilepath = ConfigurationManager.AppSettings.Get("LogFilePath");
Logger.Init(logfilepath + "CatiaLicenseUsageReportTool.log");
DBPath = ConfigurationManager.AppSettings.Get("DBFile");
objFileIO = new FileIO();
objUtilities = new Utilities();
objDBIO = new DBIO();
}
DataTable dt = new DataTable();
private void FrmCatiaLicenseUsageReport_Load(object sender, EventArgs e)
{
//Create Headers for the dataTable
dt.Columns.Add("Department", typeof(string)).ReadOnly = true;
dt.Columns.Add("AllottedLicense", typeof(int));
LicenseAllctnGridView.DataSource = dt;
//Add Rows details to the datatable
dt.Rows.Add("AASI", 7);
dt.Rows.Add("EAPS", 9);
txtWeek.Enabled = false;
}
private void btnUpdtLicens_Click_1(object sender, EventArgs e)
{
var UpdateDB = objDBIO.UpdateLicenseUsageDataFromDG(DBPath);
UpdateDB = (DataTable)LicenseAllctnGridView.DataSource;
MessageBox.Show("Data Updated Successfully.");
}
namespace CatiaLicenseUsageReport.IO
{
public class DBIO
{
/// <summary>
///
/// </summary>
/// <returns></returns>
#region Read DataBase File Function
///
public DBIO()
{
_cnStr = string.Empty;
_cmd = null;
_cnn = null;
}
public void Reset()
{
_cnStr = string.Empty;
_cmd = null;
_cnn = null;
}
private string _cnStr;
private OleDbCommand _cmd;
private OleDbConnection _cnn;
private OleDbDataAdapter _oda;
/// <summary>
/// Get Coonection for given file
/// </summary>
/// <param name="fileName"></param>
public bool GetConnectionToDb(string dbName)
{
try
{
_cmd = null;
if (_cnn != null)
_cnn.Close();
_cnStr = GetConnectionString(dbName);
_cnn = new OleDbConnection(_cnStr);
_cnn.Open();
return true;
}
catch (OutOfMemoryException)
{
return false;
}
catch (Exception)
{
return false;
}
}
private string GetConnectionString(string dbName)
{
string cnnStr;
string provider;
provider = "Microsoft.Jet.OLEDB.4.0";
cnnStr = "Provider=" + provider + ";Data Source=" + dbName + ";";
return cnnStr;
}
public DataTable ImportLicenseUsageDataFromDB(string dbName)
{
string query;
DataTable resultDt = new DataTable();
try
{
GetConnectionToDb(dbName);
//Retrive Data
_oda = new OleDbDataAdapter();
query = String.Format("select Department,AllottedLicense from AllottedLicense");
_cmd = new OleDbCommand(query, _cnn);
_oda.SelectCommand = _cmd;
_oda.Fill(resultDt);
}
catch (Exception ex)
{
Console.WriteLine("Error in function ImportLicenseUsageDataFromDB" + ex.StackTrace);
Logger.OutLine("Error in function ImportLicenseUsageDataFromDB" + ex.StackTrace);
throw ex;
}
finally
{
_cmd = null;
if (_cnn != null)
_cnn.Close();
//cnn = null;
}
return resultDt;
}
public DataTable UpdateLicenseUsageDataFromDG(string dbName)
{
string query;
DataTable resultDt = new DataTable();
try
{
// _cnn.Open();
GetConnectionToDb(dbName);
query = String.Format("select Department,AllottedLicense from AllottedLicense");
_cmd = new OleDbCommand(query, _cnn);
//Retrive Data
_cmd.ExecuteNonQuery();
_oda = new OleDbDataAdapter(_cmd);
_oda.Update(resultDt);
//_oda.UpdateCommand = _cnn.CreateCommand();
//_oda.UpdateCommand.CommandText = query;
//if(_oda.UpdateCommand.ExecuteNonQuery()>0)
//{
// _oda.SelectCommand = _cmd;
// _oda.Update(resultDt);
//}
//query = String.Format("select Department,AllottedLicense from AllottedLicense");
//_cmd = new OleDbCommand(query, _cnn);
//_oda.SelectCommand = _cmd;
//_oda.Fill(resultDt);
}
catch (Exception ex)
{
Console.WriteLine("Error in function ImportLicenseUsageDataFromDB" + ex.StackTrace);
Logger.OutLine("Error in function ImportLicenseUsageDataFromDB" + ex.StackTrace);
throw ex;
}
finally
{
_cmd = null;
if (_cnn != null)
_cnn.Close();
//cnn = null;
}
return resultDt;
}