我的 PostgreSQL 資料庫中有下表:
Table "public.ads"
Column | Type | Collation | Nullable | Default
---------------------- ----------------------------- ----------- ---------- -----------------------------------
idad | integer | | not null | nextval('ads_idad_seq'::regclass)
uidowner | integer | | |
month | integer | | |
year | integer | | |
mileage | integer | | |
idmake | integer | | |
idmodel | integer | | |
idmotor | integer | | |
idbodytype | integer | | |
description | text | | |
createdat | timestamp without time zone | | not null |
optionalequipmentids | character varying[] | | |
photos | character varying[] | | |
price | integer | | |
generaldata | jsonb | | |
vehicledata | jsonb | | |
engineenvironment | jsonb | | |
conditionmaintenance | jsonb | | |
idfueltype | integer | | |
Indexes:
"ads_pk" PRIMARY KEY, btree (idad)
Foreign-key constraints:
"ads_idbodytype_fkey" FOREIGN KEY (idbodytype) REFERENCES bodytype(idbodytype) ON UPDATE CASCADE
"ads_idfueltype_fkey" FOREIGN KEY (idfueltype) REFERENCES fueltype(idfueltype) ON UPDATE CASCADE
"ads_idmake_fkey" FOREIGN KEY (idmake) REFERENCES make(idmake) ON UPDATE CASCADE
"ads_idmodel_fkey" FOREIGN KEY (idmodel) REFERENCES model(idmodel) ON UPDATE CASCADE
"ads_idmotor_fkey" FOREIGN KEY (idmotor) REFERENCES motor(idmotor) ON UPDATE CASCADE
我創建了用上表中的資料填充 DataGridView 的方法,這里是代碼部分:
private void FillDataGridAds()
{
if (!connected) return;
string cmdString = string.Empty;
try
{
cmdString = "SELECT * FROM ads";
NpgsqlCommand command = new NpgsqlCommand(cmdString, conn);
NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridAds.DataSource = dt.DefaultView;
}
catch (NpgsqlException ex)
{
textBox1.AppendText("PostgreSQL exception: " ex.Message Environment.NewLine);
}
catch (Exception ex)
{
textBox1.AppendText("Exception ex: " ex.Message Environment.NewLine);
}
}
Designer part of code of my dataGridAds
//
// dataGridAds
//
this.dataGridAds.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGridAds.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridAds.ContextMenuStrip = this.contextMenuStrip1;
this.dataGridAds.Location = new System.Drawing.Point(9, 49);
this.dataGridAds.Name = "dataGridAds";
this.dataGridAds.RowHeadersWidth = 51;
this.dataGridAds.RowTemplate.Height = 29;
this.dataGridAds.Size = new System.Drawing.Size(1326, 549);
this.dataGridAds.TabIndex = 0;
this.dataGridAds.CellEndEdit = new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridAds_CellEndEdit);
//
Problem:
For some reason when method for filling dataGridAds is triggered, dataGridAds is displaying all columns except optionalequipmentids and photos, so does anybody know where can be a problem, I was trying to figured out where problem might be, but unsuccessfully, have same issue with another tables in my appplication and want to fix as soon as possible, thank you all in advance.
uj5u.com熱心網友回復:
這些列都是資料型別character varying[]我假設其他表中存在相同問題的列是相同的資料型別。
您可以嘗試使用CAST. 以下帶有問題列的帶有 CASE 的命名 SELECT 的腳本是否有效?
如果CAST( ... AS VARCHAR(1000))不起作用,您也可以嘗試
CAST( ... AS VARCHAR)
private void FillDataGridAds()
{
if (!connected) return;
string cmdString = string.Empty;
try
{
cmdString =
"SELECT
idad ,
uidowner ,
month ,
year ,
mileage ,
idmake ,
idmodel ,
idmotor ,
idbodytype ,
description ,
createdat ,
CAST( optionalequipmentids AS VARCHAR(1000)),
CAST( photos AS VARCHAR(1000)),
price ,
generaldata ,
vehicledata ,
engineenvironment ,
conditionmaintenance ,
idfueltype FROM ads";
NpgsqlCommand command = new NpgsqlCommand(cmdString, conn);
NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridAds.DataSource = dt.DefaultView;
}
catch (NpgsqlException ex)
{
textBox1.AppendText("PostgreSQL exception: " ex.Message Environment.NewLine);
}
catch (Exception ex)
{
textBox1.AppendText("Exception ex: " ex.Message Environment.NewLine);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/455879.html
標籤:c# .net postgresql winforms
上一篇:遞回映射問題
