| | |
How to create mysql db programmably
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
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?
•
•
Join Date: Jun 2009
Posts: 449
Reputation:
Solved Threads: 82
1
#2 Nov 10th, 2009
Try this.
You need to download MySQL Connector/Net to use the MySql.Data.MySqlClient namespace.
C# Syntax (Toggle Plain Text)
using System; using System.Drawing; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace MySqlDemo { /// <summary> /// Description of Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { void btnCreateDB(object sender, System.EventArgs e) { MySqlConnection conn = new MySqlConnection("Data Source=localhost;Persist Security Info=yes;UserId=root; PWD=YourPassword;"); MySqlCommand cmd = new MySqlCommand("CREATE DATABASE YourDBName;", conn ); conn .Open(); cmd .ExecuteNonQuery(); conn .Close(); } } }
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.
•
•
Join Date: Jul 2009
Posts: 969
Reputation:
Solved Threads: 207
0
#3 Nov 10th, 2009
Example of create table and ALTER table using the MySqlClient already mentioned:
C# Syntax (Toggle Plain Text)
public static void AlterTableExample() { string connStr = DbWrapper.TestDbWrapper.BuildConnectionString(DbWrapperType.MySql); string createStatement = "CREATE TABLE Test (Field1 VarChar(50), Field2 Integer)"; string alterStatement = "ALTER TABLE Test ADD Field3 Boolean"; using (MySqlConnection conn = new MySqlConnection(connStr)) { conn.Open(); // create the table... using (MySqlCommand cmd = new MySqlCommand(createStatement, conn)) { cmd.ExecuteNonQuery(); } // alter table to add another column... using (MySqlCommand cmd = new MySqlCommand(alterStatement, conn)) { cmd.ExecuteNonQuery(); } } }
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.
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
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- How to CREATE a mySQL database in C# code (C#)
- check if mysql table exists (PHP)
- SMS Message With PHP/MYSQL (PHP)
- create mysql query (C++)
- Pasing texte file to create mysql database using java (Java)
- How do I create a search engine on active Intranet site using MySQL and PHP (MySQL)
- MySQL: Spam Free (Send/Receive) Emails. HOW? (MySQL)
- MicroOLAP Database Designer for MySQL (Post your Resume)
- Cannot install phpBB on Win2K & MySql (Social Media and Online Communities)
Other Threads in the C# Forum
- Previous Thread: GIS
- Next Thread: Evenhandler Problem
| Thread Tools | Search this Thread |
Tag cloud for c#, mysql
.net 3.5 access advice array asp asp.net avltree beginner broadcast buyouts c# c++ calendar casting chat check combobox concurrency connect custom data database datagrid date datetime development directrobot display drawing dubai enabled error excel file form forms gdi+ geometry google html httpwebrequest image index inline insert java javascript keywords linux list listbox login matching math messagebox montywidenius multiple mysql mysqlquery object odp.net opendatabasealliance operator oracle password photoshop php picturebox post procedure programming query region remote remoting resource richtextbox saving search select server smoobjects sql sql-server sqlserver stored string sun table taborder textbox treeview update upload vb view webbrowser winforms wpf






