gpt4 book ai didi

C#和SQL数据复制

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

我有从 SQL 服务器中提取信息并显示有关汽车信息的代码。

问题是数据显示了但是重复了。 SQL 数据没有重复,但列表框显示相同的数据两次。

代码如下:

private void Show_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=XXXX");
DataTable dt = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter("SELECT Number_Plate,Registered_Keeper,Make,Model,Year_Of_Make,Colour,Engine_Size,Transmission,Fuel_Type FROM datatable WHERE Number_Plate like '%" + label1.Text + "%'", conn);
SDA.Fill(dt);
if (SDA.Fill(dt) == 0)
{

button5.Visible = false;
button4.Visible = true;
label11.Visible = true;
Show.Visible = false;
button3.Visible = true;
Make.Visible = false;
Model.Visible = false;
Year_Of_Make.Visible = false;
Colour.Visible = false;
Engine_Size.Visible = false;
Transmission.Visible = false;
Fuel_Type.Visible = false;
Registered_Keeper.Visible = false;
Plate.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
label6.Visible = false;
label7.Visible = false;
label8.Visible = false;
label9.Visible = false;
label12.Visible = false;
}
else
{
Plate.DataSource = dt;
Plate.DisplayMember = "Number_Plate";

Make.DataSource = dt;
Make.DisplayMember = "Make";

Model.DataSource = dt;
Model.DisplayMember = "Model";

Year_Of_Make.DataSource = dt;
Year_Of_Make.DisplayMember = "Year_Of_Make";

Colour.DataSource = dt;
Colour.DisplayMember = "Colour";

Engine_Size.DataSource = dt;
Engine_Size.DisplayMember = "Engine_Size";

Transmission.DataSource = dt;
Transmission.DisplayMember = "Transmission";

Fuel_Type.DataSource = dt;
Fuel_Type.DisplayMember = "Fuel_Type";

Registered_Keeper.DataSource = dt;
Registered_Keeper.DisplayMember = "RegIstered_Keeper";

Show.Visible = false;
button3.Visible = true;
}

}

注意:当我在 SQL 中添加数据时,主键丢失了大约 20 个数字,所以我的数据是 20 辆车,但是当我添加一个新数据时,它的主键大约是 50。

最佳答案

好吧,你做了两次SDA.Fill(dt)。尝试删除行中 if 之前的那个。

...
SqlDataAdapter SDA = ...
if (SDA.Fill(dt) == 0)
...

关于C#和SQL数据复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15510327/

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