C#窗口表单应用程序和SQL [英] C# window form app and SQL

查看:65
本文介绍了C#窗口表单应用程序和SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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。如果要选择其他列,则将它们添加到列表中。

In your SELECT statement you're just selecting RaceNumber from the list of columns in your table. If you want to select other columns then add them to the list.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆