How to create mysql db programmably

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 14
Reputation: SAINTJAB is an unknown quantity at this point 
Solved Threads: 0
SAINTJAB SAINTJAB is offline Offline
Newbie Poster

How to create mysql db programmably

 
0
  #1
Nov 10th, 2009
Hi, I want to create a mysql table in a database in C#. this table should have unlimited fields where the user can add more information to it without having to overwrite the already existing information. I want to do this cos I will not know the amount of info the user will add in the table and this will give the user the ability to add as much info as and when it is available without the need to restructure the table. can I do that programmably and how?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 449
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training
 
1
  #2
Nov 10th, 2009
Try this.

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using MySql.Data.MySqlClient;
  5.  
  6. namespace MySqlDemo
  7. {
  8. /// <summary>
  9. /// Description of Form1.
  10. /// </summary>
  11. public class Form1 : System.Windows.Forms.Form
  12. {
  13.  
  14. void btnCreateDB(object sender, System.EventArgs e)
  15. {
  16. MySqlConnection conn = new MySqlConnection("Data Source=localhost;Persist Security Info=yes;UserId=root; PWD=YourPassword;");
  17. MySqlCommand cmd = new MySqlCommand("CREATE DATABASE YourDBName;", conn );
  18.  
  19. conn .Open();
  20. cmd .ExecuteNonQuery();
  21. conn .Close();
  22. }
  23. }
  24. }

You need to download MySQL Connector/Net to use the MySql.Data.MySqlClient namespace.
Last edited by Ramesh S; Nov 10th, 2009 at 6:44 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 969
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 207
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #3
Nov 10th, 2009
Example of create table and ALTER table using the MySqlClient already mentioned:

  1. public static void AlterTableExample()
  2. {
  3. string connStr = DbWrapper.TestDbWrapper.BuildConnectionString(DbWrapperType.MySql);
  4. string createStatement = "CREATE TABLE Test (Field1 VarChar(50), Field2 Integer)";
  5. string alterStatement = "ALTER TABLE Test ADD Field3 Boolean";
  6.  
  7. using (MySqlConnection conn = new MySqlConnection(connStr))
  8. {
  9. conn.Open();
  10.  
  11. // create the table...
  12. using (MySqlCommand cmd = new MySqlCommand(createStatement, conn))
  13. {
  14. cmd.ExecuteNonQuery();
  15. }
  16.  
  17. // alter table to add another column...
  18. using (MySqlCommand cmd = new MySqlCommand(alterStatement, conn))
  19. {
  20. cmd.ExecuteNonQuery();
  21. }
  22. }
  23. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 447
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 86
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
0
  #4
Nov 10th, 2009
Just a thought, but wouldnt it be better to have a seperate table for the additional fields in a zero-to-many relationship?
Something like:

TABLE OF ITEMS
ItemID numeric,
ItemDesc nvarchar(max)

TABLE OF ATTRIBUTES
AttributeID numeric,
ItemID numeric,
AttributeName nvarchar(max),
AttributeType nvarchar(50),
AttributeValue nvarchar(max)

ItemID and AttributeID as seeded identity keys, ItemID becomes a foreign key link to the attributes table. That way you have an item with a description and can add as many additional attributes as you like.

The problem with doing it by adding columns (as above), is that every time you add a column, it will add an empty cell in that column for all existing items. If thats what you intend then you can ignore this post

Obviously, if you just want to set the columns from code the first time the database is created and then they are static after that then use the code above. But if you intend for the user to add a varying amount of data to each item in the table then this way will be far more efficient and flexible.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Reply

Tags
c#, mysql

Message:


Thread Tools Search this Thread



Tag cloud for c#, mysql
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC