gpt4 book ai didi

c# - 使用 C# 读取 sql 查询

转载 作者:太空宇宙 更新时间:2023-11-03 17:10:58 27 4
gpt4 key购买 nike

<分区>

我试图读取我的 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();
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com