在类中返回SqlCommand的结果

时间:2013-08-21 10:52:57

标签: c# sql sql-server winforms

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

我在想它看起来像这样:

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

我很抱歉安静的低质量问题,希望不要低估。

2 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分: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());
}