none
Connection SQL RRS feed

  • Question

  • Bonjour a tous

    Voici mon problème.

    Je tente de Créer un base de données SQL Dynamiquement mais ca fonctionne pas

    voici ce que j'utilise

     private static string sql = null;
            private static readonly string subPath = Application.StartupPath + "\\Data\\";
            private static readonly string m_DataFileName = Application.ProductName+".mdf";
            private static readonly string path = subPath + m_DataFileName;
            private static readonly string connectionString = "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=Master;Database="+path+";Trusted_Connection=True";
    
    static public void CreateDB()
            {  
                SqlConnection cn = new SqlConnection(connectionString);
                if (File.Exists(path))
                {
                    if (cn.State == ConnectionState.Closed)
                    {
                        cn.Open();
                    }
                    SqlCommand cmd;
                    sql = "DROP DATABASE " + path;
                    cmd = new SqlCommand(sql, cn);
                    cmd.ExecuteNonQuery();
                }
                else
                {
                    if (cn.State == ConnectionState.Closed)
                    {
                        cn.Open();
                    }
                    SqlCommand cmd;
                    sql = "create Database "+path;
                    cmd = new SqlCommand(sql, cn);
                    cmd.ExecuteNonQuery();
                }
            }
     

    Je reçois des erreurs que je ne maitrise pas en SQL

    comme celle-ci

    System.Data.SqlClient.SqlException
      HResult=0x80131904
      Message=Cannot open database "\bin\Debug\Data\WFADM1.mdf" requested by the login.

    The login failed.
    Login failed for user 'DESKTOP-SK1STT5\%%%%.
      Source=.Net SqlClient Data Provider
      Arborescence des appels de procédure :
       à System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
       à System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       à System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       à System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       à System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       à System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       à System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       à System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       à System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
       à System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       à System.Data.SqlClient.SqlConnection.Open()
       à WFADM1.MClass.CreateDB() dans WFADM1\WFADM1\MClass.cs :ligne 62
       à WFADM1.MForm.CreateDB_Click(Object sender, EventArgs e) dans C:\Users\morai\source\repos\WFADM1\WFADM1\MForm.cs :ligne 40
       à System.Windows.Forms.Control.OnClick(EventArgs e)
       à System.Windows.Forms.Button.OnClick(EventArgs e)
       à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       à System.Windows.Forms.Control.WndProc(Message& m)
       à System.Windows.Forms.ButtonBase.WndProc(Message& m)
       à System.Windows.Forms.Button.WndProc(Message& m)
       à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       à System.Windows.Forms.Application.Run(Form mainForm)
       à WFADM1.Program.Main() dans WFADM1\WFADM1\Program.cs :ligne 27

    Pouvez-vous m'aider SVP

    J'ai déjà passer au travers de presque tout ce j'ai trouver sur le net 

    MERCI!


    Daniel


    mardi 5 mars 2019 15:50

Toutes les réponses