system.data.dll中发生了'system.data.sqlclient.sqlexception'类型的第一次机会异常 [英] A first chance exception of type 'system.data.sqlclient.sqlexception' occurred in system.data.dll

查看:199
本文介绍了system.data.dll中发生了'system.data.sqlclient.sqlexception'类型的第一次机会异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在c#中开发一个桌面应用程序,我在调试程序时遇到了这个错误。

我想将数据从窗口应用程序发送到Web应用程序的数据库。我连接了网络应用程序数据库。这是第二种形式,它包含一个表单ei登录表单。所以请帮助我



我尝试过:



< pre lang =c#> public void timer1_Tick( object sender,EventArgs e)
{
tik--;
if (tik == 0
{
timer1。停止();
string constring = @ 数据源= (LocalDB)\ v11.0;初始目录= D:\ WEBSITE10 \APP_DATA \INFO.MDF; Integrated Security = True;

SqlConnection cnn = new SqlConnection(constring);
尝试
{
string q = update imagetable set Image_Capture = @pic where email = @Eml;
SqlCommand scmd = new SqlCommand(q,cnn);
scmd.Parameters.AddWithValue( @ Eml .textBox2.Text);
if (pictureBox1.Image!= null
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,ImageFormat.Jpeg);
byte [] photo_array = new byte [ms.Length];
ms.Position = 0 ;
ms.Read(photo_array, 0 ,photo_array.Length);
scmd.Parameters.AddWithValue( @ pic,photo_array);

}
if (cnn.State!= ConnectionState.Open)
cnn.Open();
SqlDataReader dr = scmd.ExecuteReader();
// cnn.Close();
webcam.Stop();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}

解决方案

这里有很多事情要注意:这个是一个计时器Tick事件处理程序,所以它很可能经常发生,而且经常发生。而且你不要关闭或处理你的SQL连接,这意味着它们会一直存在,直到Garbage收集器出现它们为止。这可能是下周,或下个月,或者......

和连接是稀缺的资源 - 你将耗尽很多,比内存耗尽并触发GC更快。在你的连接和命令对象上使用使用块来确保它们在超出范围时被处置。



这导致了第二个问题:作为第一个问题的结果,当您退出时,您将打开一个活动的SqlDataReader - 因此当您尝试进行下一次更新时,您可能会遇到问题。也可以在阅读器上使用使用块。



第三个是:你为什么要使用一个DateReader来做更新?改为使用ExecuteNonQuery。



但主要的是:你为什么要在计时器中这样做呢?特别是当您尝试更新和读取图像时,这可能需要相当长的时间,如果刻度周期很频繁,您将垄断数据库达到愚蠢程度。

只有更新数据库才能更新事情发生了变化,而不是计时器!


I am developing a desktop application in c# where i got this error while debugging my program .
I want to send the data from window application to the database of web application .And I connected the web application database.This is the second form it contain one more form e.i login form. So please kindly help me

What I have tried:

public void timer1_Tick(object sender, EventArgs e)
{
    tik--;
    if (tik == 0)
    {
        timer1.Stop();
        string constring = @"Data Source=(LocalDB)\v11.0;Initial Catalog=D:\WEBSITE10\APP_DATA\INFO.MDF;Integrated Security=True";

        SqlConnection cnn = new SqlConnection(constring);
        try
        {
            string q = "update imagetable set Image_Capture = @pic where email = @Eml";
            SqlCommand scmd = new SqlCommand(q, cnn);
            scmd.Parameters.AddWithValue("@Eml", this.textBox2.Text);
            if (pictureBox1.Image != null)
            {
           MemoryStream     ms = new MemoryStream();
                pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
                byte[] photo_array = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(photo_array, 0, photo_array.Length);
                scmd.Parameters.AddWithValue("@pic", photo_array);

            }
            if (cnn.State != ConnectionState.Open)
                cnn.Open();
            SqlDataReader dr = scmd.ExecuteReader();
          //  cnn.Close();
            webcam.Stop();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

解决方案

There are a number of things to note here: This is a timer Tick event handler, so the chances are that it occurs quite often, and quite frequently. And you don;t close or Dispose your SQL connections, which means they stay in existence until the Garbage collector comes along to remove them. Which may be next week, or next month, or ...
And Connections are scarce resources - you will run out of them a lot sooner than you will run out of memory and trigger the GC. Use a using block on your connection and command objects to ensure they are Disposed when they go out of scope.

And that leads to the second problem: as a result of the first problem you have an active SqlDataReader open when you exit - so you may well get problems from that when you try to do the next update. Use a using block on the reader as well.

And the third is: why the heck are you using a DateReader to do an update? Use ExecuteNonQuery instead.

But the main thing is: why are you doing this in a timer anyway? Particularly when you try to update and read images, this can take considerable time, and you will be monopolising your database to a stupid degree if the tick period is at all frequent.
Only ever update your DB if something has changed, not in a timer!


这篇关于system.data.dll中发生了'system.data.sqlclient.sqlexception'类型的第一次机会异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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