winform项目连接数据库用chart画柱状图

帮我看看这么链接数据库能不能读取到数据,我运行了感觉读不到数据,这个怎么改额,也不报错... 帮我看看这么链接数据库能不能读取到数据,我运行了感觉读不到数据,这个怎么改额,也不报错 展开
 我来答
类人界异
2018-01-04 · TA获得超过1313个赞
知道小有建树答主
回答量:349
采纳率:73%
帮助的人:76.2万
展开全部
给你个示例代码,自己对照下,你数据库链接没open,command也没有执行
private static void ConnectToData(string connectionString)
{ //Create a SqlConnection to the Northwind database.
using (SqlConnection connection = new SqlConnection(connectionString))
{ //Create a SqlDataAdapter for the Suppliers table.
SqlDataAdapter adapter = new SqlDataAdapter(); // A table mapping names the DataTable.
adapter.TableMappings.Add("Table", "Suppliers"); // Open the connection.
connection.Open();
Console.WriteLine("The SqlConnection is open."); // Create a SqlCommand to retrieve Suppliers data.
SqlCommand command = new SqlCommand( "SELECT SupplierID, CompanyName FROM dbo.Suppliers;",
connection);
command.CommandType = CommandType.Text; // Set the SqlDataAdapter's SelectCommand.
adapter.SelectCommand = command; // Fill the DataSet.
DataSet dataSet = new DataSet("Suppliers");
adapter.Fill(dataSet); // Create a second Adapter and Command to get
// the Products table, a child table of Suppliers.
SqlDataAdapter productsAdapter = new SqlDataAdapter();
productsAdapter.TableMappings.Add("Table", "Products");

SqlCommand productsCommand = new SqlCommand( "SELECT ProductID, SupplierID FROM dbo.Products;",
connection);
productsAdapter.SelectCommand = productsCommand; // Fill the DataSet.
productsAdapter.Fill(dataSet); // Close the connection.
connection.Close();
Console.WriteLine("The SqlConnection is closed."); // Create a DataRelation to link the two tables
// based on the SupplierID.
DataColumn parentColumn =
dataSet.Tables["Suppliers"].Columns["SupplierID"];
DataColumn childColumn =
dataSet.Tables["Products"].Columns["SupplierID"];
DataRelation relation = new System.Data.DataRelation("SuppliersProducts",
parentColumn, childColumn);
dataSet.Relations.Add(relation);
Console.WriteLine( "The {0} DataRelation has been created.",
relation.RelationName);
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式