|
-
March 18th, 2010, 12:48 PM
#1
How do I: C# class for database
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...");
}
}
-
March 18th, 2010, 05:16 PM
#2
Re: How do I: C# class for database
Put your code between [C0DE] put code here [/C0DE] tags - Replace the zero with a letter 0. It will then be laid out correctly and easier to read.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
March 18th, 2010, 07:50 PM
#3
Re: How do I: C# class for database
 Originally Posted by MBGustavo
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? 
Code:
DBCommand =
" SELECT"
+ " [Name]"
+ " ,[Address_1]"
+ " FROM"
+ " [Company]"
;
Why not simple
Code:
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
Last edited by JonnyPoet; March 18th, 2010 at 07:53 PM.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|