private void AttachDataBase()
{
string Str_mdf;
string Str_ldf;
string SqlCommandText; Str_mdf = "E:\\DataBase\\Test_Data.MDF";
Str_ldf = "E:\\DataBase\\Test_log.LDF"; SqlCommandText = "EXEC sp_attach_db @dbname = N'"
+ "Lisun_Test" + "',"
+ "@filename1 = N'" + Str_mdf + "',"
+ "@filename2 = N'" + Str_ldf + "'"; SqlCommand thisCommand = new SqlCommand(); thisCommand.CommandType = System.Data.CommandType.StoredProcedure;
thisCommand.Connection = SQLConn; //全局SQLConn已经正常打开
thisCommand.CommandText = SqlCommandText; thisCommand.ExecuteNonQuery(); //为何执行到这一句话就出错,我把SqlCommandText中的文本拷贝到sqlserver2005中运行正常.
{
string Str_mdf;
string Str_ldf;
string SqlCommandText; Str_mdf = "E:\\DataBase\\Test_Data.MDF";
Str_ldf = "E:\\DataBase\\Test_log.LDF"; SqlCommandText = "EXEC sp_attach_db @dbname = N'"
+ "Lisun_Test" + "',"
+ "@filename1 = N'" + Str_mdf + "',"
+ "@filename2 = N'" + Str_ldf + "'"; SqlCommand thisCommand = new SqlCommand(); thisCommand.CommandType = System.Data.CommandType.StoredProcedure;
thisCommand.Connection = SQLConn; //全局SQLConn已经正常打开
thisCommand.CommandText = SqlCommandText; thisCommand.ExecuteNonQuery(); //为何执行到这一句话就出错,我把SqlCommandText中的文本拷贝到sqlserver2005中运行正常.
改成SQL试试。存储过程不是这么用的。
class Program
{
static void Main(string[] args)
{
string mdf = @"C:\test.mdf";
string ldf = @"C:\test_log.ldf";
string cmd = string.Format("sp_attach_db");
SqlConnection connection = new SqlConnection("Server=(local); Database=master; Integrated Security=SSPI");
connection.Open();
SqlCommand command = new SqlCommand();
command.Parameters.Add(new SqlParameter("dbname", "Liu"));
command.Parameters.Add(new SqlParameter("filename1", mdf));
command.Parameters.Add(new SqlParameter("filename2", ldf));
command.Connection = connection;
command.CommandText = cmd;
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
}
}
这样不是更好吗?
干嘛要多那么多语句?