Click to See Complete Forum and Search --> : How do I: C# class for database


MBGustavo
March 18th, 2010, 12:48 PM
Hello:

I have some code that I would like to create a class for it. Below is some of my code. I hope I am posting correctly, its my first time here.

Main program:
Currently I have this to call the connection:
private void buttonDBConnect_Click(object sender, EventArgs e)
{

DBServer = textBoxProfileServer.Text;
DBDatabase = textBoxProfileDatabase.Text;
DBLogin_ID = textBoxProfileLogin_ID.Text;
DBPassword = textBoxProfilePassword.Text;
//
ClassDB.Connection(DBServer, DBDatabase, DBLogin_ID, DBPassword);
}

This fills my dataGrid:
private void buttonLoadGrid_Click(object sender, EventArgs e)
{
DBCommand =
" SELECT"
+ " [Name]"
+ " ,[Address_1]"
+ " FROM"
+ " [Company]"
;
ClassDB.Select(DBCommand);
//
SqlDataAdapter DBDataAdapter = new SqlDataAdapter(ClassDB.DBSelect, ClassDB.DBConnect);
DataSet DBDataSet = new DataSet();
DBDataAdapter.Fill(DBDataSet);
//
dataGridViewGrid.DataSource = DBDataSet.Tables[0];
dataGridViewGrid.Refresh();
}

Class:
This does the connection:
public static void Connection(string DBServer, string DBDatabase, string DBLogin_ID, string DBPassword)
{
//MessageBox.Show("DBClass: Connection");
DBConnect =
@"Server=" + DBServer + ";"
+ "Database=" + DBDatabase + ";"
+ "User ID=" + DBLogin_ID + ";"
+ "Password=" + DBPassword + ";";
//MessageBox.Show("DBConnect: With:" + DBConnect);
//
try
{
//MessageBox.Show("TRY...");
SqlConnection DBConnection = new SqlConnection();
DBConnection.ConnectionString = DBConnect;
DBConnection.Open();
DBConnection.GetSchema();
}
catch
{
//MessageBox.Show("CATCH...");
}
finally
{
//MessageBox.Show("FINALLY...");
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

What I would like to do is to call my DBClass.LoadGrid. By calling it like this: DBClass.LoadGrid(DBCommand);
This is what I have in my DBClass.LoadGrid now. What should I have in it?

public static void LoadGrid(string DBCommand)
{
//MessageBox.Show("DBLoadGrid: With:" + DBCommand);
//
try
{
//MessageBox.Show("TRY...");
DBLoadGrid = DBCommand;
}
catch
{
//MessageBox.Show("CATCH...");
}
finally
{
//MessageBox.Show("FINALLY...");
}
}

rliq
March 18th, 2010, 05:16 PM
Put your code between put code here tags - Replace the zero with a letter 0. It will then be laid out correctly and easier to read.

JonnyPoet
March 18th, 2010, 07:50 PM
Hello:

I have some code that I would like to create a class for it. Below is some of my code. I hope I am posting correctly, its my first time here.

Hmm.. lol ;)
Normally this is the way someone would say, I have some wheels, I like to build a car around them.

Standard is we dont have some code to fill it into a class.
At first we have a project, That has some purpose wherein some goals are to be fufilled. Then we have a brilliant idea how to solve that specific requirements and that leads us to a plan in that we then may break this into different sub purposes and subtargets which are to be reached... something along that way.

I never heard I have some code need to get it into a class ????
sounds like I have some Icecream do you have a cone for it? :D
DBCommand =
" SELECT"
+ " [Name]"
+ " ,[Address_1]"
+ " FROM"
+ " [Company]"
;
Why not simple
DBCommand = " SELECT [Name], [Address_1] FROM [Company]";
Easier to read and there is no real difference. A bit strange sounds for me that it takes the data from a table called 'Company'
because the table normally would hold lots of companies, so if I would design that database I would name the table 'companies' Got it ?
So my question is: where did you get the code? Out of some book and mixed up ?
There are some correct things like ´try, catch, finally, but not single one using command, which would be much easier to be handled in your case. Some of the code seems to be part of an already existing class, but sorry its all a bit wired.

Maybe you post the class or classes and not only some parts of it like the connection code