What is the point: I write my own class provider to work with the database, but an error occurs.
namespace Tourism.Data.Providers { public class DatabaseProvider { // Common database stuff public DbConnection Connection; public string ConnectionString = "Data Source=CRUST\\SQLEXPRESS;Initial Catalog=Tourism;Integrated Security=True"; public bool ConnectionOpened; public string Query; public void Connect(string connectionString) { ConnectionString = connectionString; Connection = new SqlConnection(ConnectionString); Connection.Open(); if (Connection.State.ToString() == "Open") ConnectionOpened = true; else ConnectionOpened = false; } public void SQLWithoutFetch(string query) { if (!ConnectionOpened) Connect(ConnectionString); Query = query; using (Connection) { try { SqlCommand command = new SqlCommand(Query); command.ExecuteNonQuery(); } catch (SqlException) { throw new Exception("Bad SQL query"); } } Connection.Close(); } }
When the command.ExecuteNonQuery();
string is executed command.ExecuteNonQuery();
an error occurs
ExecuteNonQuery: Connection property not initialized
Connecting to the database is going on, as indicated by the MessageBox
with the Connection.State.ToString();
.
I tried these solutions:
- make a full connection inside the
SQLWithoutFetch
method - change the query to the database (my current - "USE Places;")
- I tried to do a singleton class and statics
Thanks in advance for your help.