簡體   English   中英

c# connectionstring 屬性尚未初始化

[英]c# connectionstring property has not been initialized

我已經構建了一個 DataGridView 並很好地讀取了 sql 表。 但是數據無法通過SqlDataAdapter.Update()更新回 SQL Server,出現錯誤:

連接字符串屬性尚未初始化

這是我的代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace test_2
{
  public partial class Form1 : Form
  {
    SqlDataAdapter sda;
    DataSet ds;
    BindingSource bind1 = new BindingSource();
    SqlCommandBuilder scb;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        using (SqlConnection cn = new     SqlConnection(Properties.Settings.Default.ConnString))
        {
            ShowData();
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sda);
            bind1.DataSource = ds;
        }
    }

    private void ShowData()
    {
        using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.ConnString))
        {
           sda = new SqlDataAdapter("select key_seq, po_no, ref_no from mpo_master", cn);
            ds = new DataSet();
            sda.Fill(ds, "MPO");
            dataGridView1.DataSource = ds.Tables["MPO"]; 

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.ConnString))
            {
        scb = new SqlCommandBuilder(sda);
                sda.Update(ds, "MPO");
                MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
  }
}

這是配置文件:

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
     <configSections>
         <sectionGroup name="applicationSettings"       type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="VGB_Purchase.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<applicationSettings>
    <VGB_Purchase.Properties.Settings>
        <setting name="connString" serializeAs="String">
            <value>Data Source=vgb-angus;Initial Catalog=VGB_Purchase;Persist Security Info=True;User ID=sa;Password=jessie</value>
        </setting>
    </VGB_Purchase.Properties.Settings>
</applicationSettings>

您的代碼存在一些問題 - 未使用的連接實例,但最重要的是,您沒有打開連接!

例如

private void ShowData()
{
    using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.ConnString))
    {
       cn.Open();
       sda = new SqlDataAdapter("select key_seq, po_no, ref_no from mpo_master", cn);
       ds = new DataSet();
       sda.Fill(ds, "MPO");
       dataGridView1.DataSource = ds.Tables["MPO"]; 
       cn.Close();
    }
}

重新訪問所有函數並刪除未使用的cn初始化,例如

private void Form1_Load(object sender, EventArgs e)
{
    ShowData();
    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sda);
    bind1.DataSource = ds;
}

另外我假設sda是一個類變量? 不容易從你的代碼中確定,我上面的例子假設它是。

最后,你絕對需要異常處理; 我的示例只是插入您丟失的 Open 調用。 但是,您需要處理無法打開連接的時間。

注意:這只是關於如何根據給定的代碼結構處理數據庫調用的一個示例。 相應地適應更新。

上面的編碼風格看起來不太好

在您的代碼中,即使在更新中創建連接對象,也未設置連接屬性

如果您正在嘗試創建類級對象,請不要在 form_load 中創建連接對象時使用“using”關鍵字(這不是一個好方法-但現在可以解決您的問題-您應該使用某種架構)

要銷毀對象,您可能必須在頁面卸載時調用 dispose(這也不是一個好方法)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM