0

由于我没有长时间编程,我想问你是否有办法调用这个 SqlCommand 的结果,它在类名为klientClass

我在想它可能看起来像这样:

private static void ReadFirma()
    {
        string queryString =
            "SELECT rocnik from FIRMA;";
        using (SqlConnection connection = new SqlConnection(myConnection.DataSource.ConnectionString
                   ))
        {
            SqlCommand command = new SqlCommand(
                queryString, connection);
            connection.Open();

            int result= Convert.ToInt32(command.ExecuteScalar());
            try
            {

            }
            finally
            {

                reader.Close();
            }
        }
    }

因为我需要在此处将此结果插入到我的报告参数中:

 this.klientTableAdapter.Fill(this.prehled_zajezdu.HereReturnResult);

        this.reportViewer1.RefreshReport();

对于安静的低质量问题,我很抱歉,希望不要收到反对票。

4

2 回答 2

1

这是您可以在方法中检索和使用数据库中的值的Fill方式(前提是 Fill 方法采用该类型的参数int并且该myConnection字段可从静态方法获得)

private static int ReadFirma()
{
    string queryString = "SELECT rocnik from FIRMA";
    using (var connection = 
        new SqlConnection(myConnection.DataSource.ConnectionString))
    using(var command = new SqlCommand(queryString, connection))
    {
        connection.Open();
        return Convert.ToInt32(command.ExecuteScalar());
    }
}


void SomeMethod()
{
    this.klientTableAdapter.Fill(ReadFirma());
}
于 2013-08-21T11:05:20.933 回答
0

您可以将 DataTable 对象用于您的目标。

private static DataTable ReadFirma()
{
    string queryString = "SELECT rocnik from FIRMA";
    using (var connection = 
        new SqlConnection(myConnection.DataSource.ConnectionString))
    using(var command = new SqlCommand(queryString, connection))
    {
        connection.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = command;
        da.Fill(dt);
        return dt;
    }
}


void SomeMethod()
{
    this.klientTableAdapter.Fill(ReadFirma());
}
于 2013-08-21T11:17:21.667 回答