Visual Studio c#和SQL Server:连接属性还没有初始化

时间:2021-10-20 16:27:16

I'm trying to connect to a local database (service-based database created in Visual Studio 2013).

我正在尝试连接到一个本地数据库(Visual Studio 2013中创建的基于服务的数据库)。

I use this C# code:

我使用c#代码:

string connectionstring = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"|DataDirectory|\\InvDB.mdf\";Integrated Security=True";

public int testpripojeni()
{
    using(SqlConnection pripojeni  = new SqlConnection(connectionstring))
    {
        pripojeni.Open();

        SqlCommand prikaz = new SqlCommand();
        prikaz.CommandText = " SELECT COUNT (*) FROM HlavniTab";

        int pocet = (int)prikaz.ExecuteScalar();

        pripojeni.Close();
        return pocet;
    }
}

This function should connect to the database and count rows in table HlavniTab. But I get error on line

这个函数应该连接到数据库并在表HlavniTab中计数行。但是我在网上得到了错误

int pocet = (int)prikaz.ExecuteScalar();

It says

它说

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

类型系统的未处理异常。发生在System.Data.dll InvalidOperationException”

Additional information: ExecuteScalar: Connection property has not been initialized.

附加信息:ExecuteScalar:尚未初始化连接属性。

What should I do to fix it?

我该怎么做才能修好它?

2 个解决方案

#1


2  

your sqlcommand was not assigned a connection

您的sqlcommand没有被分配到一个连接。

try

试一试

    prikaz.Connection = pripojeni;

#2


0  

Connection property in SqlCommand must have to be populated. You can set the connection property using Constructor and also directly set the value to Connection property.

必须填充SqlCommand中的连接属性。可以使用构造函数设置连接属性,也可以直接设置连接属性的值。

Try -

试一试,

using(SqlConnection pripojeni  = new SqlConnection(connectionstring))
    {

        pripojeni.Open();
        SqlCommand prikaz = new SqlCommand(pripojeni);
        prikaz.CommandText = " SELECT COUNT (*) FROM HlavniTab";
        int pocet = (int)prikaz.ExecuteScalar();
        pripojeni.Close();
        return pocet;

    }

or

using(SqlConnection pripojeni  = new SqlConnection(connectionstring))
    {

        pripojeni.Open();
        SqlCommand prikaz = new SqlCommand();
        prikaz.Connection = pripojeni;
        prikaz.CommandText = " SELECT COUNT (*) FROM HlavniTab";
        int pocet = (int)prikaz.ExecuteScalar();
        pripojeni.Close();
        return pocet;

    }

#1


2  

your sqlcommand was not assigned a connection

您的sqlcommand没有被分配到一个连接。

try

试一试

    prikaz.Connection = pripojeni;

#2


0  

Connection property in SqlCommand must have to be populated. You can set the connection property using Constructor and also directly set the value to Connection property.

必须填充SqlCommand中的连接属性。可以使用构造函数设置连接属性,也可以直接设置连接属性的值。

Try -

试一试,

using(SqlConnection pripojeni  = new SqlConnection(connectionstring))
    {

        pripojeni.Open();
        SqlCommand prikaz = new SqlCommand(pripojeni);
        prikaz.CommandText = " SELECT COUNT (*) FROM HlavniTab";
        int pocet = (int)prikaz.ExecuteScalar();
        pripojeni.Close();
        return pocet;

    }

or

using(SqlConnection pripojeni  = new SqlConnection(connectionstring))
    {

        pripojeni.Open();
        SqlCommand prikaz = new SqlCommand();
        prikaz.Connection = pripojeni;
        prikaz.CommandText = " SELECT COUNT (*) FROM HlavniTab";
        int pocet = (int)prikaz.ExecuteScalar();
        pripojeni.Close();
        return pocet;

    }