谁知道有没有类似判断文件是否存在,那种简单的判断语句呀? //查找有无相同数据库名
foreach (object o in combox1.Items)
{
if (o.ToString().Trim().Equals(combox1.Text.Trim()))
{
frm_msg frm_ms = new frm_msg();
cls_msg.msg = "有相同名字的数据库,请改用其他数据库名字!";
frm_ms.Show();
break;
}
else
{
// string itSysDb = textBox2.Text.Trim(); string MySQL = "use master;" +
"IF DB_ID(N'MyDatabase') IS NOT NULL " +
"DROP DATABASE MyDatabase;" +
"CREATE DATABASE MyDatabase " +
"ON(NAME=MyDatabase_dat,FILENAME=\"C:\\MyDatabase.mdf\",SIZE=5,MAXSIZE=10,FILEGROWTH=1) " +
"LOG ON(NAME=MyDatabase_log,FILENAME=\"C:\\MyDatabase.ldf\",SIZE=2,MAXSIZE=5,FILEGROWTH=1)"; try
{
SqlCommand MyCommand = new SqlCommand(MySQL, conn);
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("数据安装成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // 这个提示反复弹出 sdr.Close();
conn.Close();
break;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} break;
}
}
foreach (object o in combox1.Items)
{
if (o.ToString().Trim().Equals(combox1.Text.Trim()))
{
frm_msg frm_ms = new frm_msg();
cls_msg.msg = "有相同名字的数据库,请改用其他数据库名字!";
frm_ms.Show();
break;
}
else
{
// string itSysDb = textBox2.Text.Trim(); string MySQL = "use master;" +
"IF DB_ID(N'MyDatabase') IS NOT NULL " +
"DROP DATABASE MyDatabase;" +
"CREATE DATABASE MyDatabase " +
"ON(NAME=MyDatabase_dat,FILENAME=\"C:\\MyDatabase.mdf\",SIZE=5,MAXSIZE=10,FILEGROWTH=1) " +
"LOG ON(NAME=MyDatabase_log,FILENAME=\"C:\\MyDatabase.ldf\",SIZE=2,MAXSIZE=5,FILEGROWTH=1)"; try
{
SqlCommand MyCommand = new SqlCommand(MySQL, conn);
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("数据安装成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // 这个提示反复弹出 sdr.Close();
conn.Close();
break;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} break;
}
}
绑定的是DataRowView
forearch(DataRow dr in comboBox1.Items){
if(dr[0].ToString().Equals(""))
{
break;
}
else
{break;}
}
try
{
SqlConnection conn = new SqlConnection("Server=" + textBox1.Text.Trim() + ";DataBase=master;uid=" + textBox3.Text.Trim() + ";pwd=" + textBox4.Text.Trim());
SqlDataReader temDR = dbOp.getcom("select * from sysdatabases where name = 'itSys'");
bool ifcom = temDR.Read();
//必须用Read()方法读取数据
if (ifcom)
{
frm_msg frm_ms = new frm_msg();
cls_msg.msg = "有相同数据库名称,请改用其他名称!";
frm_ms.Show();
} else
{
string MySQL = "use master;" +
"IF DB_ID(N'MyDatabase') IS NOT NULL " +
"DROP DATABASE MyDatabase;" +
"CREATE DATABASE MyDatabase " +
"ON(NAME=MyDatabase_dat,FILENAME=\"C:\\MyDatabase.mdf\",SIZE=5,MAXSIZE=10,FILEGROWTH=1) " +
"LOG ON(NAME=MyDatabase_log,FILENAME=\"C:\\MyDatabase.ldf\",SIZE=2,MAXSIZE=5,FILEGROWTH=1)"; SqlCommand MyCommand = new SqlCommand(MySQL, conn);
try
{
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MessageBox.Show("成功创建数据库", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
conn.Close();
}
}
} catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);