使用C#读取sql查询
本文关键字:查询 sql 读取 使用 | 更新日期: 2023-09-27 18:00:54
我试图读取我的sql查询,但它并没有真正起作用。这一切都在工作,直到它到达查询部分,然后上线:
rst = query.ExecuteReader();
它得到一个错误:
连接属性尚未初始化。
有人知道怎么处理吗?
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
对象没有指向SqlConnection
的链接。
更换线路:
SqlCommand query = new SqlCommand();
签字人:
SqlCommand query = conn.CreateCommand();
附言:和SqlConnection
一样,SqlCommand
和SqlDataReader
也是一次性的,所以你也可以/应该使用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();
}