从SQL数据库创建图表 [英] Create chart from SQL database
问题描述
我对这部分代码有问题,给我看错:在整数类型上转换字符串ID无效。在SQL中是int类型。请问哪里有问题?谢谢
Chart1.Series(GRAF_PORUCH)。Points.AddXY(READER.GetInt32(ID),READER.GetString( VIDENCNI_CIS))
我的尝试:
私有子BUTTON1_Click(发件人为对象,e为EventArgs)处理BUTTON1.Click
Dim DOTAZ As String =SELECT * FROM TECH_PORUCHY
Dim READER As SqlDataReader
Dim ds As New DataSet
Try
CONNECTION.Open()
CMD6 =新SqlCommand(DOTAZ,CONNECTION)
READER = CMD6.ExecuteReader
当READER.Read
Chart1.Series(GRAF_PORUCH)。XValueMember =EVIDENCNI_CIS
Chart1.Series(GRAF_PORUCH)。YValueMembers = ID
Chart1.Size = New System.Drawing.Size(780,350)
Chart1.Series(GRAF_PORUCH)。Points.AddXY(READER.GetInt32(ID),READER.GetString (VIDENCNI_CIS))
En d $
CONNECTION.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
最后
CONNECTION.Dispose()
结束尝试
结束子
如果查看文档 - < a href =https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldatareader.getint32?view=netframework-4.7.2> SqlDataReader.GetInt32(Int32)方法(系统.Data.SqlClient)| Microsoft Docs [ ^ ] - 它会显示Quote:public override int GetInt32( int i);
参数
i Int32 从零开始的列序数。您正在使用字符串在你的代码
... AddXY(READER.GetInt32(ID),...注意你会遇到与<$相同的问题c $ c> GetString 。
您还将使用SELECT *返回表格中的所有列FROM TECH_PORUCHY最好明确列出要返回的列 - 它还可以使用
GetInt32
等代码更容易阅读.Eg < pre lang =vb> Dim DOTAZ As String =SELECT ID,VIDENCNI_CIS FROM TECH_PORUCHY
。
。
。
Chart1.Series( GRAF_PORUCH)。Points.AddXY(READER.GetInt32(< span class =code-digit> 0 ),READER.GetString( 1 ))
I have a problem with this part of code, show me error: convert string ID on type integer is not valid. In SQL is type int. Where is a problem please? Thank you
Chart1.Series("GRAF_PORUCH").Points.AddXY(READER.GetInt32("ID"), READER.GetString("VIDENCNI_CIS"))
What I have tried:
Private Sub BUTTON1_Click(sender As Object, e As EventArgs) Handles BUTTON1.Click Dim DOTAZ As String = "SELECT * FROM TECH_PORUCHY" Dim READER As SqlDataReader Dim ds As New DataSet Try CONNECTION.Open() CMD6 = New SqlCommand(DOTAZ, CONNECTION) READER = CMD6.ExecuteReader While READER.Read Chart1.Series("GRAF_PORUCH").XValueMember = "EVIDENCNI_CIS" Chart1.Series("GRAF_PORUCH").YValueMembers = "ID" Chart1.Size = New System.Drawing.Size(780, 350) Chart1.Series("GRAF_PORUCH").Points.AddXY(READER.GetInt32("ID"), READER.GetString("VIDENCNI_CIS")) End While CONNECTION.Close() Catch ex As Exception MessageBox.Show(ex.Message) Finally CONNECTION.Dispose() End Try End Sub
If you check the documentation - SqlDataReader.GetInt32(Int32) Method (System.Data.SqlClient) | Microsoft Docs[^] - it will show youQuote:public override int GetInt32 (int i);
Parameters
i Int32 The zero-based column ordinal.You are using a string in your code
... AddXY(READER.GetInt32("ID"), ...Note you will have the same problem with
GetString
.
You are also returning all of the columns from your table with"SELECT * FROM TECH_PORUCHY"It is better practice to explicitly list the columns you want to return - it also makes code using
GetInt32
etc easier to read.E.g.Dim DOTAZ As String = "SELECT ID, VIDENCNI_CIS FROM TECH_PORUCHY" . . . Chart1.Series("GRAF_PORUCH").Points.AddXY(READER.GetInt32(0), READER.GetString(1))
这篇关于从SQL数据库创建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!