Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
i want to create database ,tables Store procedures using C# code
 
Database Name is Text box input string
 

 
I ready i have related Database and table,Sp
 

how to create new via C# code?.
Posted 25-Dec-12 19:55pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

using System.Data.Sql;
using System.Data.SqlClient;
 

using (SqlConnection objCon = new SqlConnection("Initial Catalog=DBName; Data Source=SERVERNAME; Integrated security=SSPI; pooling=false"))
            {
                objCon.Open();
 
                SqlCommand cmd = new SqlCommand("CREATE TABLE TEST(ID INT, NAME VARCHAR(50))", objCon);
                cmd.ExecuteNonQuery();
 
                cmd = new SqlCommand("CREATE PROCEDURE SPTEST @PARAM INT AS BEGIN SELECT * FROM TEST END", objCon);
                cmd.ExecuteNonQuery();
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Created DB
 public bool CreateCompanyDatabase(string CN)
        {
            String str;
            SqlConnection myConn = new SqlConnection("Server=ESTSYS4-PC;database=master;uid=sa;pwd=est$123;");
            str = "USE MASTER CREATE DATABASE " + CN;
            SqlCommand myCommand = new SqlCommand(str, myConn);
            try
            {
                myConn.Open();
                myCommand.ExecuteNonQuery();
                MessageBox.Show("DataBase is Created Successfully", "ISELLit", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ISELLit", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            finally
            {
                if (myConn.State == ConnectionState.Open)
                {
                    myConn.Close();
                }
            }
        }
 
Script Method
 

public void CreateScript(string CN)
        {
            string SQLscript = null;
            //ISELLit_temp C:\Users\ESTSYS4\Desktop\Design\ISELLIT\UIDESIGN\ISELLIT\ISELLIT\bin\Debug\Script
            //FileInfo file = new FileInfo(@"\\ares\c$\Inetpub\wwwroot\TestArea\SQL\testsql.sql");
            FileInfo file = new FileInfo(@"C:\Users\ESTSYS4\Desktop\Design\ISELLIT\UIDESIGN\ISELLIT\ISELLIT\bin\Debug\Script\ISELLit_temp.sql");
            string script = file.OpenText().ReadToEnd();
            SQLscript = script.Replace("GO", "");
            SqlConnection conn = new SqlConnection("Server=ESTSYS4-PC;database=" + CN + ";uid=sa;pwd=est$123;");
            SqlCommand Cmd = new SqlCommand(SQLscript, conn);
            try
            {
                conn.Open();
                Cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ISELLit", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 265
1 Richard MacCutchan 239
2 OriginalGriff 208
3 Sergey Alexandrovich Kryukov 185
4 Praneet Nadkar 167
0 OriginalGriff 7,913
1 Sergey Alexandrovich Kryukov 7,232
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 24 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100