2

I'm using this code and I Expect that 3 databases with name "Test0" & "Test1" &"Test2" be created, But just one Database Is created with name "Test0". What should I do ? I'm Using first code method for creating database,My Dbcontext name = DbCn, Thanks.

private void Form1_Load(object sender, EventArgs e)
{
    DbCn db = new DbCn();
    for (int i = 0; i <= 2; i++)
    {
        try
        {
            CheckingSource("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test" + i + ";Data Source=OLIVE");
            db = new DbCn(); 

            person pr = new person() { Number = "0912", Person = "Ahmad" };
            Melliat ml = new Melliat();
            ml.melliat = "Irani"; 
            db.Melliat.Add(ml); 
            db.SaveChanges();

            pr.ID = 1;
            pr.Number = "12"; 
            pr.MelliatID = 1; 
            db.People.Add(pr); 
            db.SaveChanges();

        }
        catch (Exception en)
        {
            MessageBox.Show(en.ToString());
        }
    }
}

private void CheckingSource(string constr)
{
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
    connectionStringsSection.ConnectionStrings["DbCn"].ConnectionString = constr;
    config.Save();
    ConfigurationManager.RefreshSection("connectionStrings");
}
sniperd
  • 5,124
  • 6
  • 28
  • 44
Ashkan
  • 21
  • 5
  • Please format your code so others can easily read it. Your existing formatting lacked line breaks and consistent indentation. – Igor Sep 10 '18 at 14:48
  • Perhaps this link will help: https://stackoverflow.com/questions/9015142/creating-a-database-programmatically-in-sql-server – David Dubois Sep 10 '18 at 14:56
  • 2
    Why are you trying to create databases in a loop anyway? And why are you allowing end users to name their database? This sounds like something went very wrong in the design phase of the application. – Sean Lange Sep 10 '18 at 15:03
  • @SeanLange actually this is not my main code , this is a sample for me to learn, I am working on an accounting Project that Must have a database for each year And I want user to create Financial period , for example financial _period_2018 The database will be so busy in a year and i'm trying to make a database for each year dynamically – Ashkan Sep 10 '18 at 15:23
  • "A database for each year" is a very clear sign that the design went the direction. That means that every year you have to change all your applications and change the connection string. This is just awful and not a good way to handle data. – Sean Lange Sep 10 '18 at 15:50
  • @SeanLange what's your idea? All information for all years in one database ? – Ashkan Sep 10 '18 at 21:32
  • Yes!!! And a column to indicate which year the data is from. – Sean Lange Sep 10 '18 at 21:32
  • @SeanLange and if the Database go so busy and slow ? – Ashkan Sep 11 '18 at 07:22
  • I would urge you not to make different databases for every year to solve a performance problem you don't have. This is known as premature optimization and it almost always results in bad decisions being made. I would ask how often people are really querying old data. You can always partition your tables to isolate older data. – Sean Lange Sep 11 '18 at 13:19

0 Answers0