C# 以编程方式创建 ASP.NET 成员资格架构

发布于 2024-11-17 01:32:04 字数 138 浏览 0 评论 0原文

我想知道动态安装成员资格模式的最佳方法是什么。

现在我正在考虑的解决方案是以某种方式运行 aspnet_regsql.exe 并带有要在我的数据库连接上执行的参数。

我怎样才能做到这一点?有更好的办法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流星番茄 2024-11-24 01:32:04

好吧,我找到了一个更好的方法来完成此任务,我仍然使用 MembershipExists() 函数,但我用这个语句安装它,直到几分钟前我才知道它。

SqlServices.Install(database, SqlFeatures.All, connectionString);

Ok I found a much better way to accomplish this, I still use the MembershipExists() function, but I install it with this statement, which I didn't know of up until a couple of minutes ago.

SqlServices.Install(database, SqlFeatures.All, connectionString);
明月夜 2024-11-24 01:32:04

是这样的吗:

    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();
        }
    }

不确定它在生产环境中的表现如何。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文