c# - 在 SQL 中将 LIKE '%' 与整数一起使用

标签 c# stored-procedures datagridview textbox sql-server-2008-r2

我在 SQL Server 中有这个过程:

create procedure AutoSearchTeamByMobile
@Team_Mobile int
As
select * from Team_Info where Team_Mobile like @Team_Mobile+'%';

我在 C# 中有这段代码:

private void textBoxX4_TextChanged(object sender, EventArgs e)
    {
        int tMobile= int.Parse(textBoxX4.Text);
        SqlCommand com = new SqlCommand("AutoSearchTeamByMobile", con);
        com.Parameters.Add("@Team_Mobile", SqlDbType.Int).Value = tMobile;
        com.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = com;
        da.Fill(ds, "Team_Info");
        dataGridViewX1.DataSource = ds.Tables["Team_Info"];
    }

我有一个手机号码列表,它们保存在名为 Team_Mobile 的列中,数据类型为 int,其中一个以'711..... .' 另一个以'700......'开头,所以现在当我在 textBoxX4 中只写'7'时,所有数字(以 711 或 700 开头)都应该出现在 dataGridViewX1,但是当我写“71”时,dataGridViewX1 中只能显示以“711”开头的数字。当我写“70”时,dataGridViewX1 中应该只显示以 700 开头的数字。我没有得到我需要的东西,实际上我没有看到任何东西,就像我没有做任何事情一样。

我的需求,当我写 71 时,所有在 Team_Mobile 列中有 711 的记录应该仍然出现,而其他数字应该隐藏。

但我用 Team_Names 做到了并且它有效,因为 Team_Names 的数据类型是 nvarchar,但是 Team_Mobileint

SQL:

create procedure AutoSearchTeam
@Team_Name nvarchar (50)
As
select * from Team_Info where Team_Name like @Team_Name+'%';

C#:

    private void textBoxX1_TextChanged(object sender, EventArgs e)
    {
        string t_name = textBoxX1.Text;
        SqlCommand com = new SqlCommand("AutoSearchTeam", con);
        com.Parameters.Add("@Team_Name", SqlDbType.NVarChar, 50).Value = t_name;
        com.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = com;
        da.Fill(ds, "Team_Info");
        dataGridViewX1.DataSource = ds.Tables["Team_Info"];
    }

我的问题是:有没有办法替换 % 以使用整数?

我可以用什么替换 % 以将其与整数一起使用以获得我上面解释的内容?

对不起我的英语。

最佳答案

比如这个

SELECT * FROM Team_Info WHERE Team_Mobile LIKE CAST(@Team_Mobile AS NVARCHAR) + '%';

关于c# - 在 SQL 中将 LIKE '%' 与整数一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19716762/

相关文章:

c# - 我的正则表达式有什么问题

c# - 单声道,跨OS(Windows/Linux)Web浏览器 View

mysql - mysql在过程中嵌套查询的替代方法

c# - 当所有行的高度超过DataGridview的视觉高度时,DataGridview的ScrollBars会Crash

c# DataGridViewComboBoxColumn selectedValue 分布在整行

c# - 为什么 C# 编译器不会对具有默认参数的方法感到困惑?

c# - 如何将 ping 变量转换为 int?

php - 如何从我的存储过程中获取第二个 SELECT?

php - 如何使用存储过程的字符串 id 插入多条记录?

c# - 失去焦点时 Datagridview 单元格丢失输入值