在C#交换机问题中从SQL存储变量. [英] Storing variables from SQL in C# switch issue.

查看:56
本文介绍了在C#交换机问题中从SQL存储变量.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,它没有正确存储变量.我对switch语句不是很好,但是相信逻辑是正确的,因为此代码是别人之前建议的.如果有人能告诉我为什么不存储变量,我会非常感激.

代码:

I have a problem where its not storing the variable properly. I''m not great with switch statements but belive the logic is right since this code was suggested before by someone else. If anyone can tell me why the variables are not being stored I would be very greatfull.

Code :

string AlphaX = null, AlphaY = null, BetaX = null, BetaY = null;                

                SqlCommand userCommand = userConnection.CreateCommand();

                userCommand.CommandText = "SELECT X, Y, GroupName FROM [dbo].[Coordinates]";

                SqlDataReader reader = userCommand.ExecuteReader();

                while (reader.Read())
                {
                    string currentGroupName = reader["GroupName"].ToString();

                    switch (currentGroupName)
                    {
                        case "Alpha":
                            AlphaX = reader["X"].ToString();
                            AlphaY = reader["Y"].ToString();
                            break;
                        case "Beta":
                            BetaX = reader["X"].ToString();
                            BetaY = reader["Y"].ToString();
                            break;
                    }
                 }
                 MessageBox.Show(AlphaX); //Test the variables value.




更新---

如果我不能这样做,是否有办法从数据库中为5行中的每行请求多个命令?




Update---

If I can''t do it this way, is there a way of me asking for multiple commands from the database for each of the 5 rows?

推荐答案

代码看起来不错,除了它只处理两个值:如果数据库包含14个"Alpha"组和27个"Beta"组,它将仅显示要从数据库读取的每个组的最后一个.我可能要做的唯一一小件事就是使它不区分大小写:
The code looks OK, with the exception that it only deals with two values: If the database contains 14 "Alpha" Groups and 27 "Beta" Groups, it will only show the last one of each to be read from the database. The only small change I might make is to make it case independent:
switch (currentGroupName.ToLower())
{
    case "alpha":
        AlphaX = reader["X"].ToString();
        AlphaY = reader["Y"].ToString();
        break;
    case "beta":
        BetaX = reader["X"].ToString();
        BetaY = reader["Y"].ToString();
        break;
}




是应该做的,还是不应该做的?


共有5个组,Alpha,Beta,Gamma等.Uhmmm基本上是在说大小写为"Alpha",那么字符串AlphaX应该=数据库中的值"X".但是由于消息框,它不存储它在代码底部,显示时仍为null :("

因此,要做的第一件事就是查看要获取的数据.
switch语句上放置一个断点,然后看一步,或者在组名之后添加一行,如下所示:




What is it doing that it shouldn''t, or not doing that it should?


"There are 5 groups, Alpha, Beta, Gamma etc. Uhmmm basically the switch is saying case "Alpha" then the string AlphaX should = the value "X" from the database. However it does not store it since the messagebox at the bottom of the code still says null when displayed :("

So, the first thing to do is look at the data you are getting out.
Either put a breakpoint at the switch statement, and look then single step, or add a line just after the Group name read:

string currentGroupName = reader["GroupName"].ToString();
Console.WriteLine(currentGroupName + 
                  " (" + 
                  reader["X"].ToString() + 
                  "," +  
                  reader["Y"].ToString() +
                  ")");
switch (currentGroupName)
{

这将告诉您所获取的数据.由此您应该能够找出问题所在.
如果没有,请发布数据,然后看看.

好吧,我不好,我不小心更改了连接字段.是的,它实际上显示正常"

That will tell you what data you are getting. From that you shouldbe able to work out where the problem is.
If not, post the data and I''ll have a look.

"Ok woops, my bad I accidently changed the connection feild. Yeah it actually displays fine"

Alpha      (500       ,455       )
Beta       (56        ,34        )
Gamma      (90        ,95        )
Delta      (34        ,34        )
Omega      (64        ,6         )



你格式化了吗?还是那是怎么打印的?
组名末尾是否有空格?
如果是这样,则"Alpha"将与"Alpha"不匹配:字符串长度不同.
试试:



Did you do the formatting? Or was that how it printed?
Are there spaces at the end of the Group name?
If so then "Alpha " will not match "Alpha": the strings are different lengths.
Try:

switch (currentGroupName.ToLower().Trim())
{
    case "alpha":
        AlphaX = reader["X"].ToString();
        AlphaY = reader["Y"].ToString();
        break;
    case "beta":
        BetaX = reader["X"].ToString();
        BetaY = reader["Y"].ToString();
        break;
}


请注意,我也始终使用小写字母来消除这种可能性.


Note that I used lower case throughout to eliminate that possibility as well.


您是否尝试调试代码?
我将首先检查查询是否返回预期结果.我真的对此表示怀疑.
Did you try to debug the code?
I would first check if the query returns the expected results. I really have my doubts on it.


这篇关于在C#交换机问题中从SQL存储变量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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