English 中文(简体)
C#Q 联系问题
原标题:C# SQL connection problem

i 正在使用VS。 2008年网络,2005年有MSQ服务器,开发一个窗户申请表,但每小时一度出现新的链接或校外错误。

在我目前的法典中,这一联系在进行问答之后开启了但又充满活力的工作。 也许在建立新的行文或新的数据来源时会遇到问题,而不能满足连接“努力”

这是我的法典。

/////////
private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            //setData();
            string ConnectingString = @"Data Source=SERVER1SQLEXPRESS;Initial Catalog=Alkawthar;Integrated Security=True;Pooling=False";
            qry = "SELECT * FROM Table1";
            //reader = db.select_data(qry);
            ds1 = new DataSet();
            conn = new SqlConnection(ConnectingString);

            conn.Open(); 
            MessageBox.Show("connection opened");
            da.Fill(ds1,"Workers");
            NavigateRecords();
            MaxRows = ds1.Tables["Workers"].Rows.Count;
            string sql = "SELECT * From tblWorkers";
            da = new System.Data.SqlClient.SqlDataAdapter(sql, conn);
            conn.Close();
            MessageBox.Show("connection closed");
            conn.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show("exception");
            MessageBox.Show(ex.Message);
        }
    }
/////////////////

Fill throws an exception also when i use reader it return null although there is data in DB thanks

最佳回答

几点意见;

  1. Like Turrau and Rune say, your stuff is in the wrong order. Open connection, Execute SQL - Get raw data, close connection. Then do your counting, etc.
  2. Don t put message box or logging calls anywhere in between the connection opening and closing. This has burned me before, where the those calls can affect the SQL call because they fudge the exception details and make it difficult to trace. This is also applicable when using a SQL data reader.
  3. Put the SQL stuff in a using block.

......

    string _connStr = @"Data Source=SERVER1SQLEXPRESS;Initial Catalog=Alkawthar;Integrated Security=True;Pooling=False";
    string _query = "SELECT * FROM Workers";
    DataSet _ds = new DataSet();

    try
    {

        using (SqlConnection _conn = new SqlConnection(_connStr))
        {
            SqlDataAdapter _da = new SqlDataAdapter(_query, _conn);
            _conn.Open();
            _da.Fill(_ds);
        }

        // insert null dataset or invalid return logic (too many tables, too few columns/rows, etc here.


        if (_ds.Tables.Count == 1)
        { //There is a table, assign the name to it.
            _ds.Tables[0].TableName = "tblWorkers";
        }

        //Then work with your tblWorkers

    }
    catch (Exception ex)
    {
        Console.Write("An error occurred: {0}", ex.Message);
    }
问题回答

The most obvious problem here is that you access the SqlDataAdapter before initializing it. That will cause a null reference exception. Try to move the line da = new SqlDataAdapter(...) to the line before you do the da.Fill(...).

Edit:

无,等待! 我看到你在那里做两次问答和两次填满。 你们需要在完成第一次补选之前先启动SqlDataAdapter。 然后,你就应当排除无参照例外。

Edit再次重申:

此外,正如所评论的那样,你不需要同时使用<条码>SqlConnection。 近和<代码>SqlConnection。 处置方法。 只要你使用SqlDataAdapter,你甚至不需要填写。 关于连接的开放,对于“Fill”方法,你们都会这样做。 只要连接开始关闭,则在接通时将再次关闭。

让我看到,你在开始使用数据Adapter。 您是否收到?

da.Fill(ds1,"Workers"); 
// ...
da = new System.Data.SqlClient.SqlDataAdapter(sql, conn); 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签