我正在使用此代码,我希望创建3个名称为“ Test0”和“ Test1”和“ Test2”的数据库,但是仅创建了一个名称为“ Test0”的数据库。 我该怎么办 ?我正在使用第一种代码方法创建数据库,我的Dbcontext名称= DbCn, 谢谢。
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");
}