gpt4 book ai didi

c# - 数据行 ID 字符串与对象 ID 字符串不匹配,即使在检查调试后它们是相同的字符串

转载 作者:搜寻专家 更新时间:2023-10-30 20:47:42 24 4
gpt4 key购买 nike

我试图在我的数据库中搜索 ID 字符串列表,其中 ID 字符串等于我要创建的对象之一。正在创建的 id 是工厂设计模式,其中火车类型“express”的 ID 为“1E45”。创建此 ID 后,它会递增字母后的数字部分,然后可用于添加的下一列火车。

当使用 foreach 搜索列表时,它会从数据库中返回与尝试创建的 ID 相似的 ID。

但是当我在使用 toString 更改它们并在 IF 中匹配后尝试匹配这两个时。匹配返回 false 即使当我检查调试时它完全相同?

然后它会继续尝试添加一个具有该 ID 的新对象,但该对象已存在并崩溃。

我做错了什么?在检查匹配的值并显示 false 后,它没有意义。

这是我设置的代码:

//Create sql command variables to create new commands
SqlCommand insert = new SqlCommand();
SqlCommand checkID = new SqlCommand();

//Set the command type to text
insert.CommandType = CommandType.Text;
checkID.CommandType = CommandType.Text;

//Searches for an ID in the database that matches one that is trying to be created
checkID.CommandText = "SELECT id FROM Train WHERE id = @trainID";

//Parameters for checking ID in database
checkID.Parameters.AddWithValue("@trainID", train.TrainID);

//Set the connection for the command for the checkID sql connection
checkID.Connection = con;

//Start the connection
con.Open();

DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(checkID);

adapter.Fill(dt);

dt.Load(checkID.ExecuteReader());

//Item last = Module.
foreach (DataRow i in dt.Rows)
{
if (i.ToString() == train.TrainID.ToString())
{
MessageBox.Show("This ID already exists! " + train.TrainID);
return;
}
}

//Close the connection
con.Close();



//Set the text for the command to insert data to the database connected to
insert.CommandText = "INSERT Train (id, departure, destination, type, intermediate, departure_time, departure_date, sleeperBerth, firstClass) " +
"VALUES ( @trainID , @departure, @destination, @type, @intermediate, @dep_time, @dep_date, @sleep, @first)";

//Parameters for adding values from the train object to the database
insert.Parameters.AddWithValue("@trainID", train.TrainID);
insert.Parameters.AddWithValue("@departure", train.Departure);
insert.Parameters.AddWithValue("@destination", train.Destination);
insert.Parameters.AddWithValue("@type", train.Type);
insert.Parameters.AddWithValue("@intermediate", intStops);
insert.Parameters.AddWithValue("@dep_time", train.DepartureTime);
insert.Parameters.AddWithValue("@dep_date", train.DepartureDay);
insert.Parameters.AddWithValue("@sleep", train.SleeperBerth);
insert.Parameters.AddWithValue("@first", train.FirstClass);

//Set the connection for the command for the insert sql connection
insert.Connection = con;

//Start the connection
con.Open();

//Execute the command specified
insert.ExecuteNonQuery();

//Close the connection
con.Close();

最佳答案

听起来您需要更改专栏 id在表中trainIDENTITY列,并让数据库处理 ID 分配:

ALTER TABLE dbo.Train ALTER COLUMN id int IDENTITY(1,1); --data type guessed

然后,在您的应用程序中,您无需为 ID 生成值,也无需在 INSERT 中声明它语句(在 INSERT 的列列表中或在您的 VALUES 子句中)。

关于c# - 数据行 ID 字符串与对象 ID 字符串不匹配,即使在检查调试后它们是相同的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467977/

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