gpt4 book ai didi

c# - 在 C# 中过滤 SQL 后值为零

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:45 26 4
gpt4 key购买 nike

我是 C# 新手..

我有写函数来过滤部门。这个函数将返回idDepartment

新问题是,部门保留值“System.Windows.Forms.Label,文本:ADMIN”,这就是我得到零的原因。那么我怎样才能只接受“ADMIN”并保留在部门呢?

更新:

    public partial class frmEditStaff : Form
{

private string connString;
private string userId, department; //Department parameter coming from here
private string conString = "Datasource";


public frmEditStaff(string strUserID, string strPosition)
{
InitializeComponent();

//Pass value from frmListStaff to userID text box

tbStaffId.Text = strUserID.ToString();
userId = strUserID.ToString();
department = strPosition.ToString();


}

下面这段代码可以正常工作,没有任何问题。

public int lookUpDepart()
{
int idDepart=0;

using (SqlConnection openCon = new SqlConnection(conString))
{
string lookUpDepartmenId = "SELECT idDepartment FROM tbl_department WHERE department = '" + department + "';";
openCon.Open();
using (SqlCommand querylookUpDepartmenId = new SqlCommand(lookUpDepartmenId, openCon))
{
SqlDataReader read = querylookUpDepartmenId.ExecuteReader();

while (read.Read())
{
idDepart = int.Parse(read[0].ToString());
break;
}
}
openCon.Close();
return idDepart;
}


}

感谢您的帮助。快乐美好的一天!

最佳答案

关于您的问题:部门保留值“System.Windows.Forms.Label,文本:ADMIN”

你在这个调用中有问题 frmEditStaff(strUserID,yourLabel.ToString());

虽然你没有分享你对这个函数/构造函数的调用,但看起来你的第二个参数在这个调用中出错了。不要在这里使用 ToString()。相反,您必须使用 .Text as

frmEditStaff(strUserID,yourLabel.Text);

这将解决您的问题。我知道这一点,因为通常程序员都有犯这种错误的经验:)

关于c# - 在 C# 中过滤 SQL 后值为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12259790/

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