Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To create a database by coding in C#.I tried the following code..
C#
private void CreateSQLDatabase()
       {
           //FileInfo file = null;
           DbCommand command = null;
           int ProcessedFileCount = 0;

           try
           {
               #region ConnectToDatabase
               string connectionString = string.Empty;
               if (UserName != string.Empty && PassWord != string.Empty)
               {
                   connectionString = string.Format("Data Source={0};User ID={1};Password={2};Initial Catalog ={3}", ServerName, UserName, PassWord, DataBase);
               }
               else
               {
                   connectionString = string.Format("Data Source={0};Integrated Security=True;Initial Catalog={1};", ServerName, DataBase);
               }
               SqlCommand EMSDb = new SqlCommand();
               SqlConnection conn = new SqlConnection(connectionString);
               // WriteLog(string.Format("Regression Test ran on {0} using user \"{1}\"", DataBase, UserName), TraceEventType.Information);

               #endregion

               // Set the total number of avaliable files
               _progressEventArgs.FileCount = _fileNameList.Count;

               for (int count = 0; count < _fileNameList.Count; count++)
               {
                   if (_fileNameList[count] != null)
                   {
                       if (!_backProcess.CancellationPending)
                       {
                           string fileName = _fileNameList[count].Substring(_fileNameList[count].LastIndexOf("\\") + 1, _fileNameList[count].Length - _fileNameList[count].LastIndexOf("\\") - 1);


                           WriteLog(string.Format("Processing file: {0}", fileName), TraceEventType.Information);

                           string dbQuery;
                           using (var reader = new StreamReader(_fileNameList[count], Encoding.Default))
                           {
                               dbQuery = reader.ReadToEnd();
                           }

                           _progressEventArgs.HasError = false;

                           try
                           {
                               _progressEventArgs.ProcessedFileCount = ++ProcessedFileCount;
                               _progressEventArgs.ProcessingFileName = fileName;
                               NotifyProgress(this, _progressEventArgs);
                               SqlConnection sqlConnection = new SqlConnection(connectionString);
                               ServerConnection svrConnection = new ServerConnection(sqlConnection);
                               Server server = new Server(svrConnection);
                               server.ConnectionContext.ExecuteNonQuery(dbQuery);
                           }
                           catch (Exception exp)
                           {
                               conn.Close();
                               _progressEventArgs.ProcessingFileName = fileName;
                               _progressEventArgs.HasError = true;
                               NotifyProgress(this, _progressEventArgs);

                               if (!_hasErrors && _progressEventArgs.HasError)
                                   _hasErrors = true;


                               WriteLog(string.Format("Error while executing {0}: {1}", fileName, exp.InnerException.Message), TraceEventType.Error);


                               if (NotifyProcessError != null)
                               {
                                   DialogResult dlgResults = NotifyProcessError(this, new ProcessErrorEventArgs(fileName, ProcessedFileCount, _fileNameList.Count));

                                   if (dlgResults == DialogResult.Abort)
                                       _backProcess.CancelAsync();
                                   else if (dlgResults == DialogResult.Retry)
                                       count--;
                               }
                           }

                       }
                       else
                           _isSuccessful = false;
                   }

               }

               _isSuccessful = true;
           }
           catch (Exception exp)
           {

               WriteLog(string.Format("Error: {0}", exp.Message), TraceEventType.Error);
               _isSuccessful = false;
           }
           finally
           {
               //file = null;
               if (command != null)
               {
                   command.Dispose();
                   command = null;
               }
           }

           if (NotifyProcessComplete != null)
               NotifyProcessComplete(this, new EventArgs());
       }
Posted
Updated 19-Nov-12 19:45pm
v2
Comments
__TR__ 20-Nov-12 1:48am    
Are you saying you want to create a separate database for each employee. Why?

1 solution

Why do you want to create a complete database for each employee?
I can think of no good reason for this: it just makes processing the interrelations of data more complex.
Normally, an employee would have a row in a table with an ID, and that ID would be used to relate the date back to the particular employee.
Using a complete database is a spectacular waste of resources for no real benefit.
 
Share this answer
 

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