gpt4 book ai didi

c# - SQL查询执行难度

转载 作者:行者123 更新时间:2023-11-30 14:49:57 25 4
gpt4 key购买 nike

我在从应用程序添加学生时进行验证。如果我运行以下查询

SELECT ID FROM Student WHERE Name =' " '+str+' " '

它将产生以下错误:

Invalid column name 'str'.

我的应用程序将生成 DBException。

我该如何解决这个问题?

编辑

String SName=txtBox1.Text;

String sql="select id from student where name = ' "+SName.Trim()+" ' ";

SqlConnection connection = null;
SqlDataReader reader = null;
try
{
connection = GetConnection();
SqlCommand command = new SqlCommand(sql, connection);
if (_sqltransection != null)
{
command.Transaction = _sqltransection;
}
reader = command.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (SqlException ex)
{
throw new DBException(ex);
}

哪里 txtBox.Text="'+str+' "

最佳答案

SELECT ID FROM Student WHERE Name =' " '+'divyesh'+' " '

但是没有意义...

也许你会更喜欢这样的东西:

SELECT ID FROM Student WHERE Name like '%divyesh%'

如果要在字符串中添加单个字符:

SELECT '''hello'''

关于c# - SQL查询执行难度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38049950/

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