I was wondering what would be the best way to install the membership schema on the fly.
Right now the solution I'm thinking of is to somehow run aspnet_regsql.exe
with arguments to execute on my db connection.
How can I accomplish this? Is there a better way?
好的,我找到了一个更好的方法来实现这一点,我仍然使用MembershipExists()
函数,但我用这个语句安装它,直到几分钟前我才知道。
SqlServices.Install(database, SqlFeatures.All, connectionString);
Did it like this:
public static void Initialize()
{
var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["membership"].ConnectionString);
if (!MembershipExists(connection))
{
// create schema
string regsql = Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "aspnet_regsql.exe");
string args = string.Format(@"-E -S {0} -A all -d {1}", connection.DataSource, connection.Database);
var proc = Process.Start(regsql, args);
if (proc != null)
proc.WaitForExit();
}
}
public static bool MembershipExists(SqlConnection connection)
{
try
{
connection.Open();
var query = new SqlCommand("select count(*) from sysobjects where name = 'aspnet_CheckSchemaVersion' and type = 'P'", connection);
return query.ExecuteScalar() as int? == 1;
}
finally
{
connection.Close();
}
}
Not sure how it'll behave on a production environment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.