简体   繁体   English

Dapper 确保查询成功最佳实践

[英]Dapper ensure that query succeeded best practice

I want to use Dapper into my WebApi Project.我想在我的 WebApi 项目中使用Dapper I Have a method for creating database from query:我有一种从查询创建数据库的方法:

    public async Task<bool> CreateDatabase()
    {
        var connection = _connection.GetOpenConnection();

        bool created = await connection.ExecuteAsync(SqlQuery.CreateDatabase); 
    }

My question is, how to write query for creating database and how to ensure, that query completed succesfuly and which method from dapper to use?我的问题是,如何编写用于创建数据库的查询以及如何确保该查询成功完成以及使用 dapper 中的哪种方法?

My SqlQuery.CreateDatabase looks like this:我的SqlQuery.CreateDatabase看起来像这样:

IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'DataBase')
  BEGIN
    CREATE DATABASE DataBase
  END

Do i need to return something from query?我需要从查询中返回一些东西吗?

If a problem occurs, it should already present as an exception - probably a SqlException - telling you about it, so: what you have should already be fine.如果出现问题,它应该已经作为异常出现 - 可能是一个SqlException - 告诉你它,所以:你所拥有的应该已经没问题了。 Note, though, that if your connection string tries to connect to the database that doesn't yet exist , then Open[Async] is likely to fail.但请注意,如果您的连接字符串尝试连接到尚不存在的数据库,则Open[Async]可能会失败。 Ultimately, you could just try connecting to the specified database afterwards to be sure, but again, what you have should already be fine .最终,您可以在之后尝试连接到指定的数据库以确保,但同样,您所拥有的应该已经没问题了

Note that it is often the case that app accounts do not have permissions to create databases.请注意,应用程序帐户通常没有创建数据库的权限 In that scenario, you'll need to get a DBA to create the database for you, and tell you the connection details.在这种情况下,您需要让 DBA 为您创建数据库,并告诉您连接的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM