CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    May 2008
    Posts
    27

    [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.

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    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

  3. #3
    Join Date
    May 2008
    Posts
    27

    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.

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Need help with managing DB from code!

    Quote 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?

  5. #5
    Join Date
    May 2008
    Posts
    27

    Re: Need help with managing DB from code!

    I'm using SQL Server!

  6. #6
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Need help with managing DB from code!

    Quote 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?

  7. #7
    Join Date
    May 2008
    Posts
    27

    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.

  8. #8
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    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.

  9. #9
    Join Date
    May 2008
    Posts
    27

    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.

  10. #10
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Need help with managing DB from code!

    Quote 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.

  11. #11
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    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]);
    }

  12. #12
    Join Date
    May 2008
    Posts
    27

    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!

  13. #13
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Need help with managing DB from code!

    Quote 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.

  14. #14
    Join Date
    May 2008
    Posts
    27

    Re: Need help with managing DB from code!

    No, I'm using .Net 3.5. Nothing is added to my comboBox.

  15. #15
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Need help with managing DB from code!

    Quote 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?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured