简体   繁体   English

从C#查询SQL Server 2008 R2中的master.sys.databases视图不起作用

[英]Querying master.sys.databases view in SQL Server 2008 R2 from c# is not working

I'm using the code below to find if a database exists but the ExecuteNonQuery always returns -1. 我正在使用下面的代码来查找数据库是否存在,但是ExecuteNonQuery始终返回-1。

I've seen the master.sys.databases view and it has the database POS 我看过master.sys.databases视图,它具有数据库POS

SqlConnection tmpConn = new SqlConnection("Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");

sqlCheckDBQuery = "SELECT * FROM master.sys.databases where name = \'aspnetdb\'";

using (tmpConn)
{
    try
    {
        tmpConn.Open();
        tmpConn.ChangeDatabase("master");
    }
    catch (Exception)
    {
        MessageBox.Show("SQLServer Express Database is either not installed or not running!", "Database Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Application.Exit();
    }

    using (SqlCommand sqlCmd = new SqlCommand(sqlCheckDBQuery, tmpConn))
    {
        int exists = sqlCmd.ExecuteNonQuery();

        if (exists <= 0)
            databaseExists = false;
        else
            databaseExists = true;
    }
}

in this particular instance you can use a scalar query instead of a data reader. 在这种特定情况下,您可以使用标量查询代替数据读取器。

sqlCheckDBQuery = "SELECT count(1) FROM master.sys.databases where name = 'aspnetdb'";
var count = (int)sqlCmd.ExecuteScalar();
databaseExists = count > 1;

You should use ExecuteScalar() and change the query to run a COUNT(*) . 您应该使用ExecuteScalar()并更改查询以运行COUNT(*)

ExecuteScalar will bring back the rows affected. ExecuteScalar将带回受影响的行。

ExecuteNonQuery() will bring back -1 for SELECT 's. ExecuteNonQuery()将为SELECT返回-1。

ExecuteNonQuery() is the wrong method to use. ExecuteNonQuery()是使用错误的方法。 Use ExecuteReader() instead: eg: 使用ExecuteReader()代替:例如:

var reader = command.ExecuteReader();
if (reader.Read())
    ....
sqlCheckDBQuery = "SELECT * FROM master.sys.databases where name = \'aspnetdb\'";

Why are you using backslashes here? 为什么在这里使用反斜杠? The name of the database is aspnetdb so this should be: 数据库的名称是aspnetdb因此应为:

sqlCheckDBQuery = "SELECT * FROM master.sys.databases where name = 'aspnetdb'";

Also as has been mentioned in other answers you cannot use ExecuteNonQuery() here since that will return always -1 for select statements. 另外,正如其他答案中提到的那样,您不能在此处使用ExecuteNonQuery() ,因为对于选择语句,该值将始终返回-1。

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

相关问题 使用C#安装SQL Server 2008 R2 Express - Install SQL Server 2008 R2 Express using C# 具有现有SQL Server 2008 R2数据库的C#应用 - C# App with Existing SQL Server 2008 R2 Database 从C#程序创建新的SQL Server 2008 R2登录名 - Create new SQL Server 2008 R2 login from C# program 如何使用C#仅从SQL Server 2008 R2备份表数据 - How to backup only table data from SQL Server 2008 R2 using C# 如何在C#中将图像从SQL Server 2008 R2检索到Datagridview [Windows应用程序] - How to retrieve image from SQL Server 2008 R2 to Datagridview in C# [Windows Application] 如何通过网络从C#ClickOnce应用程序访问SQL Server 2008 R2 - How to access sql server 2008 R2 from C# ClickOnce application over a network 进行设置以使用C#连接到SQL Server 2008 R2中的服务器 - Make a setting to connect to server in SQL Server 2008 R2 using C# C#代码,用于为插入SQL Server 2008 R2的每一行生成唯一ID - C# Code for generating Unique ID for each row inserted into SQL Server 2008 R2 C#SQL Server 2008 R2插入存储过程将不起作用 - C# sql server 2008 r2 insert stored procedure won't work 尝试在SQL Server 2008 R2上调用某个列时,在C#上未处理SqlException - SqlException unhandled on C# when trying to call a certain column on SQL Server 2008 R2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM