用C#读取SQL查询

aha*_*636 1 c# sql sql-server sqldatareader fusioncharts

我试图让我的SQL查询读取,但它并没有真正起作用.它一直在工作,直到它来到查询部分,然后在线:

rst = query.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)

它收到一个错误:

Connection属性尚未初始化.

有谁知道如何处理这个?

 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();
}
Run Code Online (Sandbox Code Playgroud)

Séb*_*rin 8

您的SqlCommand对象没有链接到您的SqlConnection.

替换线:

SqlCommand query = new SqlCommand();
Run Code Online (Sandbox Code Playgroud)

通过:

SqlCommand query = conn.CreateCommand();
Run Code Online (Sandbox Code Playgroud)

PS:像SqlConnection,SqlCommand并且SqlDataReader也是一次性的,这样你就可以/也应该使用using.
这条线conn.Close();没用,因为它using会照顾它.


sul*_*din 5

一种方法

var  query  = new SqlCommand("SELECT * from table", conn);
Run Code Online (Sandbox Code Playgroud)

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

query.connection = conn;
Run Code Online (Sandbox Code Playgroud)