Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
[no name] 26-Dec-12 5:52am    
Database created Successfully then how to run Script file?..

i am tried

FileInfo file = new FileInfo(@"C:\Users\ESTSYS4\Desktop\Design\ISELLIT\UIDESIGN\ISELLIT\ISELLIT\bin\Debug\Script\ISELLit_temp.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection("Server=ESTSYS4-PC;database=" + CN + ";uid=sa;pwd=est$123;");
SqlCommand Cmd = new SqlCommand(script, conn);

conn.Open();
Cmd.ExecuteNonQuery();

 
Share this answer
 
C#
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();
            }
 
Share this answer
 
Created DB
C#
 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();
                }
            }
        }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900