使用 C# 读取 sql 查询

Read a sql query with C#

我试图让我的 sql 查询被读取,但它并没有真正起作用。一切正常,直到涉及到查询部分,然后上线:

rst = query.ExecuteReader();

出现错误:

Connection property has not been initialized.

有人知道怎么处理吗?

 Chart chart = new Chart();
    StringBuilder xmlStr = new StringBuilder();
    StringBuilder strCategories = new StringBuilder();
    StringBuilder strProcesses = new StringBuilder();
    StringBuilder strTasks = new StringBuilder();

    xmlStr.Append("<chart logoURL='../../Images/Piktogramme/" + chart.Image + "' caption='" + chart.Caption + "' theme='flat'" + " dateformat='dd/mm/yyyy' showTaskLabels='1'>"); // attributes will go here

    // Category for each month
    for (int i = -12; i < 6; i++)
    {
        DateTime today = DateTime.Now;
        today = today.AddMonths(i);
        strCategories.Append("<category start='1/" + today.Month + "/" + today.Year + "' end='" + DateTime.DaysInMonth(today.Year, today.Month) + "/" + today.Month + "/" + today.Year + "' name='" + today.ToString("MMM") + "' />");
    }

    // Get the connection string
    string connStr = ConfigurationManager.ConnectionStrings["CRM_SQL"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connStr))
    {
        // Establish the connection with the database
        conn.Open();

        // Construct and execute SQL query which would return the total amount of sales for each year
        SqlCommand query = new SqlCommand();
        // Begin iterating through the result set
        SqlDataReader rst;
        query.CommandText = "SELECT * from table";
        rst = query.ExecuteReader();


        while (rst.Read())
        {
            // Construct the chart data in XML format
            strProcesses.AppendFormat("<process name='{1}' id='{0}' />", rst[0], rst[1]);
            strTasks.AppendFormat("<task name='{0}' processid='{1}' start='{2}' end='{3}' />", rst[4], rst[0], rst[2], rst[3]);
        }
        DateTime today = DateTime.Now;
        xmlStr.Append("<trendlines><line start='" + DateTime.DaysInMonth(today.Year, today.Month) + "/" + today.Month + "/" + today.Year + "' displayvalue='Heute'/></trendlines>");
        // End the XML string
        xmlStr.Append("<categories>" + strCategories.ToString() + "</categories> <processes>" + strProcesses.ToString() + "</processes> <tasks width='10'>" + strTasks.ToString() + "</tasks> </chart>");

        // Close the result set Reader object and the Connection object
        rst.Close();
        conn.Close();
    }

    return xmlStr.ToString();
}

您的 SqlCommand 对象没有 link 到您的 SqlConnection

替换行:

SqlCommand query = new SqlCommand();

作者:

SqlCommand query = conn.CreateCommand();

PS:和SqlConnection一样,SqlCommandSqlDataReader也是一次性的,所以你can/should也用using
conn.Close(); 行没用,因为 using 会处理它。

一种方法;

var  query  = new SqlCommand("SELECT * from table", conn);

另一种方法是分配连接字符串

query.connection = conn;

添加以下内容

query.Connection=conn;

之后

SqlCommand query = new SqlCommand();

试试这个:

using (SqlConnection conn = new SqlConnection(connStr))
{
    // Establish the connection with the database
    conn.Open();

    using (SqlCommand query = new SqlCommand("SELECT * from table", conn))
    {
        query.CommandType = CommandType.Text;
        using (var rst = query.ExecuteReader())
        {
            while (rst.Read())
            {
                strProcesses.AppendFormat("<process name='{1}' id='{0}' />", rst[0], rst[1]);
                strTasks.AppendFormat("<task name='{0}' processid='{1}' start='{2}' end='{3}' />", rst[4], rst[0], rst[2], rst[3]);
            }
        }
    }
    DateTime today = DateTime.Now;
    xmlStr.Append("<trendlines><line start='" + DateTime.DaysInMonth(today.Year, today.Month) + "/" + today.Month + "/" + today.Year + "' displayvalue='Heute'/></trendlines>");
    // End the XML string
    xmlStr.Append("<categories>" + strCategories.ToString() + "</categories> <processes>" + strProcesses.ToString() + "</processes> <tasks width='10'>" + strTasks.ToString() + "</tasks> </chart>");
    conn.Close();
}