问题描述
使用C#Windows Form App时出现sql查询问题。
I am having an issue with a sql query when using C# Windows Form App
string CB =" Car193";
CB = textBox1.Text;
$
使用(SqlCommand cmd = new SqlCommand(" select RaceNumber FROM [蓝色]。[黄色]。[绿色]其中[RacetNumber] in('CB')",con)))
这只会调出比赛号码但不是(CAR193)
string CB = "Car193";
CB = textBox1.Text;
using (SqlCommand cmd = new SqlCommand("select RaceNumber FROM [blue].[yellow].[green] where [RacetNumber] in ('CB')", con))
This will just bring up the Race Number but not the (CAR193)
我做错了什么?
推荐答案
SELECT RaceNumber, Column2, Column3 FROM table WHERE condition
在尝试在代码中使用它们之前在SSMS中测试您的查询以确保您获得所需的数据。
Test your queries in SSMS before trying to use them in code to ensure you're getting the data you want.
此外您的WHERE条件不会看起来对我来说。你正在寻找等于CB的RacetNumber。您实际上对您定义的CB变量没有任何作用。使用参数化查询将数据插入查询。
Also your WHERE condition doesn't look right to me. You're looking for RacetNumber that is equal to CB. You're actually doing nothing with the CB variable you defined. Use parameterized queries to insert data into a query.
using (var cmd = new SqlCommand("SELECT RaceNumber FROM table WHERE RaceNumber = @number", con))
{
cmd.Parameters.AddWithValue("@number", CB);
};
这将选择RaceNumber等于CB变量中任何值的行。
This will select rows where RaceNumber is equal to whatever value is in your CB variable.
这篇关于C#窗口表单应用程序和SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!