下面是按钮还原事件,运行出来for循环里面那个cmd.ExecuteNonQuery();
调试报这句错误'.' 附近有语法错误。高手请教下.
private void btnHuan_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Server=.;Database=master;uid=sa;pwd=worinima");
con.Open();
StringBuilder sql = new StringBuilder("select spid from master..sysprocesses where dbid=db_id('Oder') ");
SqlCommand cmd = new SqlCommand(sql.ToString(), con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
ArrayList list = new ArrayList();
while (dr.Read())
{
list.Add(dr.GetInt16(0));
}
dr.Close();
for (int i = 0; i < list.Count; i++)
{
cmd = new SqlCommand(string.Format("KILL {0}", list), con);
cmd.ExecuteNonQuery();
}
try
{
string BackupFile = this.txtServer.Text;
StringBuilder strSQL = new StringBuilder("use master;restore database SanHai From disk = @BackupFile with replace");
SqlCommand cmdOpen = new SqlCommand(strSQL.ToString(), con);
cmdOpen.Parameters.Add(new SqlParameter(@"BackupFile", SqlDbType.NVarChar));
cmdOpen.Parameters[@"BackupFile"].Value = BackupFile;
cmdOpen.ExecuteNonQuery();
MessageBox.Show("还原数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}