none
create table in sql server while running a program RRS feed

  • Question

  • i want to create table while running a program. am corrently using C#.net and sql server 2005

     

    Wednesday, October 19, 2011 4:14 AM

Answers

  • Use the SqlConnection class to open a connection to SQL Server.

    Then use the SqlCommand class to execute the create table statement, using the ExecuteNonQuery method.

    Here is a simple example -> http://www.java2s.com/Code/CSharp/Database-ADO.net/usetheExecuteNonQuerymethodtorunDDLstatements.htm

     

     


    Matt
    Wednesday, October 19, 2011 6:11 AM
    Moderator
  • Hi ullas1000,


    I suggest you to learn ado.net first, you can learn it from here:

    http://msdn.microsoft.com/en-us/library/s7ee2dwt(v=VS.71).aspx


     

    learn the main classes of the ado.net: DBConnection, DBCommand, DBDataAdapter, DBDataReader, then you can use C# to do some insert, update, delete and select command with your database.


     

    These links are about using C# to operate Sql Server:


     

    SqlConnection:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx

     

    SqlCommand:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

     

    SqlDataAdapter:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx

     

    SqlDataReader:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

     



    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ullas1000 Friday, June 14, 2013 8:22 AM
    Thursday, October 20, 2011 8:01 AM
    Moderator
  • you should add these namespace in your aspx.cs sheet 

    using System.Data.SqlClient;
    using Microsoft.SqlServer.Server;
    using System.Data.SqlTypes;

    then follow the codes below 

     string sCreateTable = "CREATE TABLE " + txtusername.Text + " (ID int PRIMARY KEY IDENTITY(1,1)," +
                        "item_code char(50),quantity int,price int,buy bit default 'true')";
                    Response.Write(sCreateTable);
                    //string sInsertFirstRow = "INSERT INTO " + txtusername.Text + "(TITLE,AUTHOR,PAGECOUNT,TOPIC,CODE)"
                    //    + "VALUES('Test Book','Test Author', 100, 'Test Topic', 'Test Code');";

                    SqlConnection mycon = new SqlConnection();
                    mycon.ConnectionString = "Data Source=.;Initial Catalog=ullas;Integrated Security=True";
                    
                    SqlCommand mycomm = new SqlCommand();
                    mycomm.CommandType = CommandType.Text;
                    mycomm.CommandText = sCreateTable;
                    mycomm.Connection = mycon;

                    try
                    {
                        //    Open the connection
                        mycon.Open();
                        //    Execute CreateDatabase query
                        SqlDataReader dr = mycomm.ExecuteReader();
                    }
                    catch (SqlException ec)
                    {
                        //    Catch any errors and show the error message
                        Response.Write("sqlexception1" + ec.Message);
                        Response.Write(" The database already exists. ");
                    }
                    finally
                    {
                        mycon.Close();
                    }


    • Marked as answer by ullas1000 Friday, June 14, 2013 8:22 AM
    Friday, June 14, 2013 8:21 AM

All replies

  • Use the SqlConnection class to open a connection to SQL Server.

    Then use the SqlCommand class to execute the create table statement, using the ExecuteNonQuery method.

    Here is a simple example -> http://www.java2s.com/Code/CSharp/Database-ADO.net/usetheExecuteNonQuerymethodtorunDDLstatements.htm

     

     


    Matt
    Wednesday, October 19, 2011 6:11 AM
    Moderator
  • Hi ullas1000,


    I suggest you to learn ado.net first, you can learn it from here:

    http://msdn.microsoft.com/en-us/library/s7ee2dwt(v=VS.71).aspx


     

    learn the main classes of the ado.net: DBConnection, DBCommand, DBDataAdapter, DBDataReader, then you can use C# to do some insert, update, delete and select command with your database.


     

    These links are about using C# to operate Sql Server:


     

    SqlConnection:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx

     

    SqlCommand:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

     

    SqlDataAdapter:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx

     

    SqlDataReader:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

     



    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ullas1000 Friday, June 14, 2013 8:22 AM
    Thursday, October 20, 2011 8:01 AM
    Moderator
  • you should add these namespace in your aspx.cs sheet 

    using System.Data.SqlClient;
    using Microsoft.SqlServer.Server;
    using System.Data.SqlTypes;

    then follow the codes below 

     string sCreateTable = "CREATE TABLE " + txtusername.Text + " (ID int PRIMARY KEY IDENTITY(1,1)," +
                        "item_code char(50),quantity int,price int,buy bit default 'true')";
                    Response.Write(sCreateTable);
                    //string sInsertFirstRow = "INSERT INTO " + txtusername.Text + "(TITLE,AUTHOR,PAGECOUNT,TOPIC,CODE)"
                    //    + "VALUES('Test Book','Test Author', 100, 'Test Topic', 'Test Code');";

                    SqlConnection mycon = new SqlConnection();
                    mycon.ConnectionString = "Data Source=.;Initial Catalog=ullas;Integrated Security=True";
                    
                    SqlCommand mycomm = new SqlCommand();
                    mycomm.CommandType = CommandType.Text;
                    mycomm.CommandText = sCreateTable;
                    mycomm.Connection = mycon;

                    try
                    {
                        //    Open the connection
                        mycon.Open();
                        //    Execute CreateDatabase query
                        SqlDataReader dr = mycomm.ExecuteReader();
                    }
                    catch (SqlException ec)
                    {
                        //    Catch any errors and show the error message
                        Response.Write("sqlexception1" + ec.Message);
                        Response.Write(" The database already exists. ");
                    }
                    finally
                    {
                        mycon.Close();
                    }


    • Marked as answer by ullas1000 Friday, June 14, 2013 8:22 AM
    Friday, June 14, 2013 8:21 AM