Would it be wise to Use a Dll to access a database When a application starts.
This is the code i have to call in every page in my app
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "Data Source=CRYSTAL5\\INSTANCE1;Initial Catalog=Pharmacy;Integrated Security=True";
myConnection.Open();
and the dll code i wrote
public class DBConnect
{
public DBConnect()
{
Initialize();
}
private SqlConnection connection;
//Constructor
//Initialize values
private void Initialize()
{
string connectionString;
connectionString = "Data Source=CRYSTAL5\\INSTANCE1;Initial Catalog=Pharmacy;Integrated Security=True";
connection = new SqlConnection(connectionString);
}
i then added using DBCon; in my app and tried to run this code
DBConnect myConnection = new DBConnect();
SqlCommand myCommand = new SqlCommand("select doc_fname,doc_lname,gender,department,education ,NMC_no from ph.doctor_info where unit_id =0", myConnection);
it won’t work.
sorry for the crappy description
The best overloaded method match for ‘System.Data.SqlClient.SqlCommand.SqlCommand(string, System.Data.SqlClient.SqlConnection)’ has some invalid arguments
is the error i get
What would i do without stack overflow.
You are passing an object of type “DbConnect” to SqlCommand rather than an SqlConnection object. Your sql connection object is held within this class, in the
connectionvariable.You will either need to make this field public, or alternatively create a function which returns the data connection.
If making connection public, you would then do:
EDIT: Also in Initialise, you have created the SqlConnection object, but not called
Open(). You will need to do this before trying to use the connection. Either addconnection.Open()to initialise(), or callmyConnection.connection.Open()prior to calling the SqlCommand.