I am new to C# and Visual Studio. I am trying to write a small application where I can Insert/Update/Delete and Select records from MySQL Database.
I do have programing experience in PHP and other languages but it is my first time with a windows application.
I wrote a class that handles the open/close connection to MySQL and also process queries.
The class name is dbConnetion
and it is located in Clases\dbConnetion.cs
On the form that I want to use this class I put on the top
using dbConnetion;
but for some reason I keep getting an error on the way I am trying to include the class
Error 1 The type or namespace name 'dbConnection' could not be found (are you missing a using directive or an assembly reference?) C:\Users\User\C# Projects\POS\POS\newDepartment.cs
here is a screenshot on what I am running into
This is a screenshot of the files
here is my class code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace POS
{
public class dbConnetion
{
//private OdbcConnection conn;
private readonly string mServer;
private readonly string mDatabase;
private readonly string mUid;
private readonly string mPassword;
private readonly string mPort;
private readonly string conn_string;
public dbConnetion()
{
mServer = "localhost";
mDatabase = "pos";
mUid = "root";
mPassword = "";
mPort = "3306";
conn_string = String.Format("server={0};user={1};database={2};port={3}password={4};", mServer, mUid, mDatabase, mPort, mPassword);
}
//Start connection to database
private bool startConnection(MySqlConnection mConnection)
{
try
{
mConnection.Open();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
return false;
}
}
//Close connection
private bool closeConnection(MySqlConnection mConnection)
{
try
{
mConnection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public MySqlDataReader getDataSet(string query)
{
MySqlConnection conn = new MySqlConnection(conn_string);
if (startConnection(conn) == true)
{
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader dataset = cmd.ExecuteReader();
closeConnection(conn);
}
return dataset;
}
public void processQuery(string strSQL, List<MySqlParameter> pars)
{
MySqlConnection conn = new MySqlConnection(conn_string);
if (startConnection(conn) == true)
{
MySqlCommand cmd = new MySqlCommand(strSQL, conn);
foreach (MySqlParameter param in pars)
{
cmd.Parameters.Add(param);
}
cmd.ExecuteNonQuery();
closeConnection(conn);
}
}
}
}
How do I correct this issue? and how do I include the class correctly?
There are multiple problems here:
using
directive for a class. You can't do that (before C# 6, anyway) - you either specify an alias for a class (which you don't want here) or you just specify a namespace in order to import all the types in that namespace. (So that you can refer to them by their simple names.)dbConnetion
for some reason, but you've got using dbConnection;
POS
namespace, but you're trying to use it without specifying a namespaceFundamentally, you don't need a using
directive for it at all as you're trying to use it from code in the same namespace. It does need to be in a project though - either the same one as you're trying to use it from, or a referenced projects.
You should also read up on .NET naming conventions - none of your names follow them at the moment.
Also, it's a bad idea to keep a connection around for any length of time - you should open it, use it, close it. Use a using
statement to make sure you close it at the end, even if an exception is thrown.
See more on this question at Stackoverflow