从数据库读取错误,但没有错误消息 [英] Read from database error but no error message

查看:81
本文介绍了从数据库读取错误,但没有错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我遇到了一个问题,Visual Studio不会抛出任何错误,但是代码会抛出错误.当try和catch语句就位时,它们将抛出错误并停止程序.但是,当我尝试并捕获程序负载等时,代码却按预期运行.我可能会以一种完全不合常规的方式来查看它,这就是问题所在,但对我而言,它似乎应该可以工作.

下面是sql代码部分,以及我的调用方式.

Hi there, I have a problem where visual studio throws no error but the code does. When the try and catch statements are inplace they throw back the error and the program stops. However when I take out the try and catch the program loads etc but the code does do as I am expecting. I may be looking at this in a totally unorthadox way and thats the problem but to me it looks like it should work.

Below is the sql code section and also how I am calling it.

public void getUser()
       {
           SqlConnection conn = new SqlConnection(Calorie_Counter_UK.Properties.Settings.Default.Calorie_CounterConnectionString);
           conn.Open();
         try
          {
               using (SqlCommand command = new SqlCommand("SELECT * FROM [User] LIMIT 1",conn))
               {
                   SqlDataReader reader = command.ExecuteReader();
                   while (reader.Read())
                   {
                       name = reader.GetString(0);
                       age = reader.GetInt16(1);
                       height = reader.GetInt16(2);
                       weight = reader.GetDecimal(3);
                       target = reader.GetDecimal(4);
                   }
               }
         }
         catch
        {
             MessageBox.Show("Problem with update");
        }
       }
       public string getName()
       {
           return name;
       }
       public int getAge()
       {
           return age;
       }
       public int getHeight()
       {
           return height;
       }
       public decimal getWeight()
       {
           return weight;
       }
       public decimal getTarget()
       {
           return target;
       }





private void Window_Loaded(object sender, RoutedEventArgs e)
       {
           User_database user = new User_database();
           user.getUser();
           txtName.Text = user.getName();
           nudAge.Value = user.getAge();
           nudHeight.Value = user.getHeight();
           nudWeight.Value = Convert.ToDouble(user.getWeight());
           nudTargetWeight.Value = Convert.ToDouble(user.getTarget());
       }



这让我有些沮丧,因为我无法收到错误消息甚至无法帮助我!如果有人可以阐明一些想法,将不胜感激!

Dan



It is frustrating me a little bit because I can''t get an error message to even help me along! If someone could shed some light it would be appreciated!

Dan

推荐答案

为什么不将SqlConnection放在try块中?

我将与调试器一起逐步检查它的状态.
Why aren''t you putting the SqlConnection stuff inside the try block?

I would step through it with the debugger to see what''s happening.


此代码应告诉您可能是什么问题:
This code should tell you what the problem might be:
public void getUser()
{
	SqlConnection conn = new SqlConnection(Calorie_Counter_UK.Properties.Settings.Default.Calorie_CounterConnectionString);
	using (SqlCommand command = new SqlCommand("SELECT TOP 1 * FROM [User] ",conn))
	{
		try
		{
			command.Connection.Open();
			SqlDataReader reader = command.ExecuteReader();
			while (reader.Read())
			{
				name = reader.GetString(0);
				age = reader.GetInt16(1);
				height = reader.GetInt16(2);
				weight = reader.GetDecimal(3);
				target = reader.GetDecimal(4);
			}
		}
		catch (Exception err)
		{
			MessageBox.Show(err.ToString()); // provide that message in 
		}
		finally
		{
			if (command.Connection.State.Equals(ConnectionState.Open))
			{
				command.Connection.Close();
				command.Connection.Dispose();
			}
		}
	}
}


您还没有提到它是什么数据库.如果它是SQL Server(如您使用的SqlConnection类),则按照digimanus的建议,将查询重写为
You have not mentioned what database it is. If it is SQL Server (as you have used SqlConnection class) then, as digimanus suggested, rewrite your query as
SELECT TOP 1 * FROM [User]


MS SQL Server中没有LIMIT子句.

如果数据库确实是MySQL,则使用适当的库来访问数据库,在这种情况下,不能使用SqlConnection.

而且您的try/catch代码太可怕了.如果完全使用try/catch,请使用catch块记录实际的异常消息.


There is no LIMIT clause in MS SQL Server.

If you database is indeed MySQL, then use the appropriate Library to access the database, you cannot use SqlConnection in that case.

And your try/catch code is horrible. If at all you use try/catch, use the catch block to log the actual exception message.


这篇关于从数据库读取错误,但没有错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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