tags:

views:

52

answers:

2

I m trying to create database programticaly using c#. i created scripts from datbase creation. which works when i run it from sqlserver mngmt studio. but when i ru same scripts from c3 application follwing error occurs.

An unhandled exception of type 'Microsoft.SqlServer.Management.Common.ExecutionFailureException' occurred in Microsoft.SqlServer.ConnectionInfo.dll

what could be the reason?????

A: 

Just to let you know there is a c# frame work which will do all this for you. http://www.sharparchitecture.net/ it will build your DB and your data access layer from a OO model.

TheAlbear
A: 

public string CreateDataBase(string ipAddress, string UserName, string Password,string DB_filepath) {

        Microsoft.SqlServer.Management.Smo.Server addDBserver = new     Microsoft.SqlServer.Management.Smo.Server(ipAddress);
        addDBserver.ConnectionContext.LoginSecure = false;
        addDBserver.ConnectionContext.Login = UserName;
        addDBserver.ConnectionContext.Password = Password;



        try
        {
            //*Crerate Databse*
            addDBserver.ConnectionContext.Connect();
            FileInfo filedb = new FileInfo(DB_filepath);
            string scriptdb = filedb.OpenText().ReadToEnd();
            string scriptdb1 = scriptdb.Replace("GO", Environment.NewLine);
            string scriptdb2 = scriptdb1.Replace("\r\nGO\r\n", "");
            addDBserver.ConnectionContext.ExecuteNonQuery(scriptdb2);
            addDBserver.ConnectionContext.Disconnect();
            string Msg;
                Msg = "db created successfully";
                return Msg;
            return true;


        }
        catch (Exception ex)
        {
       string Msg1 = "db notcreated successfully";
         return ex.Message;
           throw;
        }
    }
        //Database created Successfully
rajshades