private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "")
{
try
{
string P_Str_cmdtxt = "USE master DECLARE tb CURSOR LOCAL FOR SELECT 'Kill '+ CAST(Spid AS VARCHAR) FROM master.dbo.sysprocesses";
P_Str_cmdtxt += " WHERE dbid=DB_ID('storage') DECLARE @s nvarchar(1000) OPEN tb FETCH tb INTO @s";
P_Str_cmdtxt += " WHILE @@FETCH_STATUS = 0 BEGIN EXEC (@s) FETCH tb INTO @s END CLOSE tb DEALLOCATE tb";
P_Str_cmdtxt += " RESTORE DATABASE storage FROM disk='" + P_Str_BakPath + "'";
if (GetCon().State == ConnectionState.Open)
{
GetCon().Close();
G_Con.Close();
try
{
//string MyConn = "server=643715FF64F2450\\SQLEXPRESS;Trusted_Connection=yes;user=sa;pwd=123;database=master";
//MyConn.;
//SqlConnection conn = new SqlConnection("server=Listen;database=master;Uid=sa;Pwd=611118");
SqlCommand sqlcom = new SqlCommand(P_Str_cmdtxt, GetCon());
sqlcom.ExecuteNonQuery();
MessageBox.Show("数据还原成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (GetCon().State == ConnectionState.Closed)
{
GetCon().Open();
GetCon().Dispose();
// G_Con.Open();
}
}
catch
{
button1_Click(sender, e);
}
} }
catch
{
button1_Click(sender, e);
} }
else
{
MessageBox.Show("请选择备份的正确位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
数据还原出错
我有一套完整的,數據庫備份和恢復的代碼,如果要給我信息。
是(asp.net全部源码)
给你一个killallUser的代码段。
private void KillAllUser(System.Data.SqlClient.SqlConnection con)
{
System.Data.SqlClient.SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter("select spid from sysprocesses where dbid=(select dbid from sysdatabases where name like 'GoldAbacus')",con);
DataSet ds = new DataSet();
ad.Fill(ds);
DataTable dt = ds.Tables[0];
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = con;
for (int i = 0; i < dt.Rows.Count; i++)
{
cmd.CommandText = "kill " + Convert.ToString(dt.Rows[i].ItemArray[0]).Trim();
cmd.ExecuteNonQuery();
}
ad.Dispose();
ds.Dispose();
dt.Dispose();
}
我要还的数据库为"GoldAbacus",要看具体情况替换掉'GoldAbacus',就可以了