C# CLASSE BASE DADOS LOCAL SDF
Olá a todos, sou novo por aqui e estou a experimentar o vs 2012 para Desktop.
No entanto estou com dificuldades. Tenho a minha classe de dados:
using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Configuration;
using System.Collections;
using System.Web;
public class clsDatabase
{
private SqlCeConnection objConn;
private SqlCeCommand objCmd;
private SqlCeTransaction Trans;
private String strConnString;
public clsDatabase()
{
strConnString = System.Configuration.ConfigurationManager.AppSettings[[Ô]dbConnectionString[Ô]];
}
public DataSet QueryDataSet(String strSQL)
{
strConnString = System.Configuration.ConfigurationManager.AppSettings[[Ô]dbConnectionString[Ô]];
DataSet ds = new DataSet();
SqlCeDataAdapter dtAdapter = new SqlCeDataAdapter();
objConn = new SqlCeConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new SqlCeCommand();
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
return ds; //*** Return DataSet ***//
}
public void TransStart()
{
objConn = new SqlCeConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void TransExecute(String strSQL)
{
objCmd = new SqlCeCommand();
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.Transaction = Trans;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
}
public void TransRollBack()
{
Trans.Rollback();
}
public void TransCommit()
{
Trans.Commit();
}
public void Close()
{
objConn.Close();
objConn = null;
}
}
E quando tenho aceder no meu form a um grid:
clsDatabase clsDB = new clsDatabase();
string strSQLA = null;
DataSet ds;
strSQLA = [Ô]select * from clientes[Ô];
ds = clsDB.QueryDataSet(strSQLA);
this.dataGridView1.DataSource = ds;
Tenho o erro :
An unhandled exception of type [ô]System.InvalidOperationException[ô] occurred in System.Data.SqlServerCe.dll
Additional information: The ConnectionString property has not been initialized.
Costumo usar sempre vb mas agora queria fazer em C#, alguém me pode ajudar?
Cumps.
No entanto estou com dificuldades. Tenho a minha classe de dados:
using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Configuration;
using System.Collections;
using System.Web;
public class clsDatabase
{
private SqlCeConnection objConn;
private SqlCeCommand objCmd;
private SqlCeTransaction Trans;
private String strConnString;
public clsDatabase()
{
strConnString = System.Configuration.ConfigurationManager.AppSettings[[Ô]dbConnectionString[Ô]];
}
public DataSet QueryDataSet(String strSQL)
{
strConnString = System.Configuration.ConfigurationManager.AppSettings[[Ô]dbConnectionString[Ô]];
DataSet ds = new DataSet();
SqlCeDataAdapter dtAdapter = new SqlCeDataAdapter();
objConn = new SqlCeConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new SqlCeCommand();
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
return ds; //*** Return DataSet ***//
}
public void TransStart()
{
objConn = new SqlCeConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void TransExecute(String strSQL)
{
objCmd = new SqlCeCommand();
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.Transaction = Trans;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
}
public void TransRollBack()
{
Trans.Rollback();
}
public void TransCommit()
{
Trans.Commit();
}
public void Close()
{
objConn.Close();
objConn = null;
}
}
E quando tenho aceder no meu form a um grid:
clsDatabase clsDB = new clsDatabase();
string strSQLA = null;
DataSet ds;
strSQLA = [Ô]select * from clientes[Ô];
ds = clsDB.QueryDataSet(strSQLA);
this.dataGridView1.DataSource = ds;
Tenho o erro :
An unhandled exception of type [ô]System.InvalidOperationException[ô] occurred in System.Data.SqlServerCe.dll
Additional information: The ConnectionString property has not been initialized.
Costumo usar sempre vb mas agora queria fazer em C#, alguém me pode ajudar?
Cumps.
Bom dia MARCO,
Não tem inicialização do Connection:
objConn = new SqlCeConnection();
Não tem inicialização do Connection:
objConn = new SqlCeConnection();
Faça seu login para responder