2015-07-03 59 views
1

我试图让我的sql查询读取,但它并没有真正的工作。这是所有的工作,直到它来查询部分,然后就行了:用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(); 
} 
+1

同样的问题和问题在这里:http://stackoverflow.com/questions/10263094/executenonquery-connection-property-has-not-been-initialized – Xanatos

回答

8

SqlCommand对象没有链接到你的SqlConnection

将行:

SqlCommand query = new SqlCommand(); 

通过:

SqlCommand query = conn.CreateCommand(); 

PS:像SqlConnectionSqlCommandSqlDataReader也是一次性的,这样你就可以/也应该使用using
而行conn.Close();是无用的,因为using将照顾它。

4

一种方法来解决这个问题;

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

另一种方式是assinging的ConnectionString

query.connection = conn; 
3

添加以下

query.Connection=conn; 

SqlCommand query = new SqlCommand(); 
2

试试这个:

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