|
-
October 20th, 2008, 10:49 AM
#1
[RESOLVED] Need help with managing DB from code!
Hi!
First of all: I'm not 100% sure that this is the place to put my question, but I think it wouldn't fit in the ADO.Net forum.
Here's my question:
My application will need to be able to create new tables in an existing database, write data to database and read data. The problem is I have only been working with creating the tables first and then read the data. Not to create new tables from the code. And the code that I've been using has been generated by Visual Studio. I think I would be able to read data and I know how to add tables and connect. I would need to know how to add columns to a table (existing or not doesn't matters), and to fill it with data.
Last edited by Guitarcomet; October 20th, 2008 at 11:35 AM.
-
October 20th, 2008, 01:52 PM
#2
Re: Need help with managing DB from code!
you didn't really ask a question.
do you want to know how to programmatically add columns to a database table? If so, this SQL statement will add a column named "Column15" with datatype of varchar and allowing nulls to table "Table1".
Code:
ALTER TABLE Table1 ADD
Column15 varchar(25) null
-
October 21st, 2008, 02:08 AM
#3
Re: Need help with managing DB from code!
Yeah, I want to programmatically add a column. I'm trying to use your code, but it never works for me. Well, I'm new to this so I'm not quite sure on how to give the command to the database. It's an *.mdf database!
Last edited by Guitarcomet; October 21st, 2008 at 02:15 AM.
-
October 21st, 2008, 07:48 AM
#4
Re: Need help with managing DB from code!
 Originally Posted by Guitarcomet
Yeah, I want to programmatically add a column. I'm trying to use your code, but it never works for me. Well, I'm new to this so I'm not quite sure on how to give the command to the database. It's an *.mdf database!
are you using ACCESS or SQL Server?
-
October 21st, 2008, 08:41 AM
#5
Re: Need help with managing DB from code!
-
October 21st, 2008, 09:08 AM
#6
Re: Need help with managing DB from code!
 Originally Posted by Guitarcomet
I'm using SQL Server!
the code is just a fairly simple sql query that runs and works fine with SQL 2005(and I would assume works in 2000 if you are using that).
Can you post your code?
-
October 21st, 2008, 09:11 AM
#7
Re: Need help with managing DB from code!
I found a way in which I could create a table to database and create rows in it. But now I need to know if it's possible to get a list of all tables in a database and put it in a comboBox. And if it is, how it is done.
Last edited by Guitarcomet; October 21st, 2008 at 09:19 AM.
-
October 21st, 2008, 09:20 AM
#8
Re: Need help with managing DB from code!
Code:
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
DataTable dt = cn.GetSchema();
cn.Close();
then you should be able to bind the datatable to the combobox's datasource property.
-
October 21st, 2008, 09:48 AM
#9
Re: Need help with managing DB from code!
No, it doesn't work. The text in my combobox is System.Data.DataRowView. And that's not the names of tables.
-
October 21st, 2008, 11:39 AM
#10
Re: Need help with managing DB from code!
 Originally Posted by Guitarcomet
No, it doesn't work. The text in my combobox is System.Data.DataRowView. And that's not the names of tables.
then you will need to loop through the datatable to get the SQL table names.
-
October 21st, 2008, 03:29 PM
#11
Re: Need help with managing DB from code!
here is an updated version of the code above....
Code:
SqlConnection cn = new SqlConnection(Settings.GetInstance().ConnectionString.ConnectionString);
string[] restrictions = new string[4];
// name of your database
restrictions[0] = "YourDataBaseName";
// owner
restrictions[1] = "dbo";
// null because we want to return all tables
restrictions[2] = null;
// just return tables and not views
restrictions[3] = "BASE TABLE";
cn.Open();
DataTable dt = cn.GetSchema("Tables", restrictions);
cn.Close();
foreach (DataRow row in dt.Rows)
{
comboBox1.Items.Add(row.ItemArray[2]);
}
-
October 22nd, 2008, 05:16 AM
#12
Re: Need help with managing DB from code!
Sorry, eclipsed4utoo, but neither that way worked. I had to solve this problem by creating another table holding data about what tables exists. I copied your code and edited it so that it should work for me, but no. Thanks for your help anyaway!
-
October 22nd, 2008, 07:29 AM
#13
Re: Need help with managing DB from code!
 Originally Posted by Guitarcomet
Sorry, eclipsed4utoo, but neither that way worked. I had to solve this problem by creating another table holding data about what tables exists. I copied your code and edited it so that it should work for me, but no. Thanks for your help anyaway! 
either you are using .Net 1.1(which you should state), or you did something wrong. I copy and pasted that code directly from Visual Studio after I tested it. It works fine for me.
-
October 22nd, 2008, 08:17 AM
#14
Re: Need help with managing DB from code!
No, I'm using .Net 3.5. Nothing is added to my comboBox.
-
October 22nd, 2008, 08:47 AM
#15
Re: Need help with managing DB from code!
 Originally Posted by Guitarcomet
No, I'm using .Net 3.5. Nothing is added to my comboBox.
have you tried breakpointing to see what values are being added? is there a different way of adding values to a combobox in .Net 3.5?
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
|