在“员工表"中插入员工时,如何创建与员工同名的数据库 [英] How to create a database of the same name as an employee while inserting an employee in Employee Table

查看:135
本文介绍了在“员工表"中插入员工时,如何创建与员工同名的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要通过C#编码创建数据库.我尝试了以下代码..

To create a database by coding in C#.I tried the following code..

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());
       }

推荐答案

为什么要为每个员工创建完整的数据库?
我认为没有充分的理由:这只会使处理数据的相互关系更加复杂.
通常,一个员工在表中会有一个带有ID的行,并且该ID将用于将日期与特定员工相关联.
使用完整的数据库会浪费大量资源,没有任何实际好处.
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.


这篇关于在“员工表"中插入员工时,如何创建与员工同名的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆