2015-08-20 114 views
0

我已經介紹了很多教程和文章,但無法準確找到我需要的信息來幫助我解決問題。無法查看數據

我的問題是,我編譯的DLL不會將我的SQL數據庫文件中的數據行返回到DataGridView

我已經創建這個項目作爲一個Windows應用程序進行測試,它工作正常。但是,當我將輸出更改爲類庫時,它會運行,但不會將任何數據返回到DataGridView。標題在那裏,因爲它被聲明,但我有一種感覺,編譯後的dll無法建立到數據庫的連接。

我有C#和ASP的經驗,但沒有創建dll的經驗。

爲了進一步說明,我可以使用datasetbindingsource,然後使用預覽數據選項編譯和查看所有記錄。在我的情況下創建類庫時,是否有特定的方法來設置數據庫連接?

SQLDatabase.mdf連接字符串:

Data Source=(localdb)\v11.0;Initial Catalog=SQLDatabase.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False 

設計器生成的代碼:this.itemsTableAdapter.Fill(this.abDataSet.Items);

請讓我知道你是否希望我發佈Form.Designer.cs,它也是設計器生成的代碼。同樣,一切工作正常,作爲一個Windows應用程序,但作爲一個類庫,數據行不會顯示在DataGridView中。

namespace AHPriceChecker4Free 
{ 
    public class AHPriceChecker : Core 
    { 
     private Thread formThread; 

     public void PluginStop() 
     { 
      Log("closed"); 
     } 

     private AHCheckerForm ahForm; 
     public bool formIsOpen; 

     public void PluginRun() 
     { 
      formThread = new Thread(RunForm); 
      formThread.SetApartmentState(ApartmentState.STA); 
      formThread.Start(); 
      formIsOpen = true; 
      while (formIsOpen) 
      { 
       Thread.Sleep(1000); 
      } 
     } 

     private void RunForm() 
     { 
      ahForm = new AHCheckerForm(this, me); 
      System.Windows.Forms.Application.Run(ahForm); 
     } 
    } 
} 

Form.cs

namespace AHPriceChecker4Free 
{ 
    partial class AHCheckerForm 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.components = new System.ComponentModel.Container(); 
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AHCheckerForm)); 
      this.menuStrip1 = new System.Windows.Forms.MenuStrip(); 
      this.tsmMain = new System.Windows.Forms.ToolStripMenuItem(); 
      this.tsmLoad = new System.Windows.Forms.ToolStripMenuItem(); 
      this.tsmSave = new System.Windows.Forms.ToolStripMenuItem(); 
      this.txtItemName = new System.Windows.Forms.TextBox(); 
      this.btnSearch = new System.Windows.Forms.Button(); 
      this.ofdOpen = new System.Windows.Forms.OpenFileDialog(); 
      this.sfdSave = new System.Windows.Forms.SaveFileDialog(); 
      this.btnMassSearch = new System.Windows.Forms.Button(); 
      this.itemPriceHistory = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.itemAveragePrice = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.itemLastPrice = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.itemName = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.dgvAuctionTable = new System.Windows.Forms.DataGridView(); 
      this.tabControl1 = new System.Windows.Forms.TabControl(); 
      this.tabPage1 = new System.Windows.Forms.TabPage(); 
      this.tabPage2 = new System.Windows.Forms.TabPage(); 
      this.label1 = new System.Windows.Forms.Label(); 
      this.rtbSave = new System.Windows.Forms.RichTextBox(); 
      this.tabPage3 = new System.Windows.Forms.TabPage(); 
      this.label8 = new System.Windows.Forms.Label(); 
      this.label7 = new System.Windows.Forms.Label(); 
      this.label6 = new System.Windows.Forms.Label(); 
      this.label5 = new System.Windows.Forms.Label(); 
      this.label4 = new System.Windows.Forms.Label(); 
      this.label3 = new System.Windows.Forms.Label(); 
      this.label2 = new System.Windows.Forms.Label(); 
      this.tabPage4 = new System.Windows.Forms.TabPage(); 
      this.dataGridView1 = new System.Windows.Forms.DataGridView(); 
      this.idDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.itemNameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.goldDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.silverDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.copperDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.perQtyDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.abchristDataSet = new AHPriceChecker4Free.abchristDataSet(); 
      this.itemsTableAdapter = new AHPriceChecker4Free.abchristDataSetTableAdapters.ItemsTableAdapter(); 
      this.itemsBindingSource1 = new System.Windows.Forms.BindingSource(this.components); 
      this.menuStrip1.SuspendLayout(); 
      ((System.ComponentModel.ISupportInitialize)(this.dgvAuctionTable)).BeginInit(); 
      this.tabControl1.SuspendLayout(); 
      this.tabPage1.SuspendLayout(); 
      this.tabPage2.SuspendLayout(); 
      this.tabPage3.SuspendLayout(); 
      this.tabPage4.SuspendLayout(); 
      ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); 
      ((System.ComponentModel.ISupportInitialize)(this.abchristDataSet)).BeginInit(); 
      ((System.ComponentModel.ISupportInitialize)(this.itemsBindingSource1)).BeginInit(); 
      this.SuspendLayout(); 
      // 
      // menuStrip1 
      // 
      this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.tsmMain}); 
      this.menuStrip1.Location = new System.Drawing.Point(0, 0); 
      this.menuStrip1.Name = "menuStrip1"; 
      this.menuStrip1.Padding = new System.Windows.Forms.Padding(7, 2, 0, 2); 
      this.menuStrip1.Size = new System.Drawing.Size(819, 24); 
      this.menuStrip1.TabIndex = 0; 
      this.menuStrip1.Text = "menuStrip1"; 
      this.menuStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.menuStrip1_ItemClicked); 
      // 
      // tsmMain 
      // 
      this.tsmMain.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.tsmLoad, 
      this.tsmSave}); 
      this.tsmMain.Name = "tsmMain"; 
      this.tsmMain.Size = new System.Drawing.Size(41, 20); 
      this.tsmMain.Text = "&Main"; 
      // 
      // tsmLoad 
      // 
      this.tsmLoad.Name = "tsmLoad"; 
      this.tsmLoad.Size = new System.Drawing.Size(123, 22); 
      this.tsmLoad.Text = "&Load data"; 
      this.tsmLoad.Click += new System.EventHandler(this.tsmLoad_Click); 
      // 
      // tsmSave 
      // 
      this.tsmSave.Name = "tsmSave"; 
      this.tsmSave.Size = new System.Drawing.Size(123, 22); 
      this.tsmSave.Text = "&Save data"; 
      this.tsmSave.Click += new System.EventHandler(this.transferDataToSave); 
      // 
      // txtItemName 
      // 
      this.txtItemName.BackColor = System.Drawing.Color.LightYellow; 
      this.txtItemName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 
      this.txtItemName.Location = new System.Drawing.Point(8, 353); 
      this.txtItemName.Name = "txtItemName"; 
      this.txtItemName.Size = new System.Drawing.Size(203, 20); 
      this.txtItemName.TabIndex = 2; 
      // 
      // btnSearch 
      // 
      this.btnSearch.BackColor = System.Drawing.Color.LightYellow; 
      this.btnSearch.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 
      this.btnSearch.Location = new System.Drawing.Point(217, 351); 
      this.btnSearch.Name = "btnSearch"; 
      this.btnSearch.Size = new System.Drawing.Size(87, 25); 
      this.btnSearch.TabIndex = 5; 
      this.btnSearch.Text = "Search"; 
      this.btnSearch.UseVisualStyleBackColor = false; 
      this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click); 
      // 
      // ofdOpen 
      // 
      this.ofdOpen.FileName = "openFileDialog1"; 
      // 
      // sfdSave 
      // 
      this.sfdSave.FileOk += new System.ComponentModel.CancelEventHandler(this.sfdSave_FileOk); 
      // 
      // btnMassSearch 
      // 
      this.btnMassSearch.BackColor = System.Drawing.Color.LightYellow; 
      this.btnMassSearch.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 
      this.btnMassSearch.Location = new System.Drawing.Point(310, 351); 
      this.btnMassSearch.Name = "btnMassSearch"; 
      this.btnMassSearch.Size = new System.Drawing.Size(105, 25); 
      this.btnMassSearch.TabIndex = 8; 
      this.btnMassSearch.Text = "Mass search"; 
      this.btnMassSearch.UseVisualStyleBackColor = false; 
      this.btnMassSearch.Click += new System.EventHandler(this.btnMassSearch_Click); 
      // 
      // itemPriceHistory 
      // 
      this.itemPriceHistory.HeaderText = "Price History"; 
      this.itemPriceHistory.Name = "itemPriceHistory"; 
      this.itemPriceHistory.ReadOnly = true; 
      this.itemPriceHistory.Width = 350; 
      // 
      // itemAveragePrice 
      // 
      this.itemAveragePrice.HeaderText = "Average price"; 
      this.itemAveragePrice.Name = "itemAveragePrice"; 
      this.itemAveragePrice.ReadOnly = true; 
      this.itemAveragePrice.Width = 110; 
      // 
      // itemLastPrice 
      // 
      this.itemLastPrice.HeaderText = "Last price"; 
      this.itemLastPrice.Name = "itemLastPrice"; 
      // 
      // itemName 
      // 
      this.itemName.Frozen = true; 
      this.itemName.HeaderText = "Name"; 
      this.itemName.Name = "itemName"; 
      this.itemName.ReadOnly = true; 
      this.itemName.Width = 200; 
      // 
      // dgvAuctionTable 
      // 
      this.dgvAuctionTable.BackgroundColor = System.Drawing.Color.Cornsilk; 
      this.dgvAuctionTable.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
      this.dgvAuctionTable.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
      this.itemName, 
      this.itemLastPrice, 
      this.itemAveragePrice, 
      this.itemPriceHistory}); 
      this.dgvAuctionTable.Dock = System.Windows.Forms.DockStyle.Top; 
      this.dgvAuctionTable.Location = new System.Drawing.Point(3, 3); 
      this.dgvAuctionTable.Name = "dgvAuctionTable"; 
      this.dgvAuctionTable.RowHeadersVisible = false; 
      this.dgvAuctionTable.Size = new System.Drawing.Size(805, 342); 
      this.dgvAuctionTable.TabIndex = 1; 
      this.dgvAuctionTable.CurrentCellDirtyStateChanged += new System.EventHandler(this.dgvAuctionTable_CurrentCellDirtyStateChanged); 
      // 



**// *** REMOVED ALL TABS BUT THE ONE I AM HAVING AN ISSUE WITH TO REDUCE SIZE. THIS FORM BODY IS LIMITED TO 30000 CHARACTERS WHEN POSTING *** // 
      //** 



      // tabPage4 
      // 
      this.tabPage4.Controls.Add(this.dataGridView1); 
      this.tabPage4.Location = new System.Drawing.Point(4, 23); 
      this.tabPage4.Name = "tabPage4"; 
      this.tabPage4.Padding = new System.Windows.Forms.Padding(3); 
      this.tabPage4.Size = new System.Drawing.Size(811, 381); 
      this.tabPage4.TabIndex = 3; 
      this.tabPage4.Text = "Database"; 
      this.tabPage4.UseVisualStyleBackColor = true; 
      // 
      // dataGridView1 
      // 
      this.dataGridView1.AllowUserToAddRows = false; 
      this.dataGridView1.AllowUserToDeleteRows = false; 
      this.dataGridView1.AutoGenerateColumns = false; 
      this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
      this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
      this.idDataGridViewTextBoxColumn, 
      this.itemNameDataGridViewTextBoxColumn, 
      this.goldDataGridViewTextBoxColumn, 
      this.silverDataGridViewTextBoxColumn, 
      this.copperDataGridViewTextBoxColumn, 
      this.perQtyDataGridViewTextBoxColumn}); 
      this.dataGridView1.DataSource = this.itemsBindingSource1; 
      this.dataGridView1.Location = new System.Drawing.Point(66, 69); 
      this.dataGridView1.Name = "dataGridView1"; 
      this.dataGridView1.ReadOnly = true; 
      this.dataGridView1.RowHeadersVisible = false; 
      this.dataGridView1.Size = new System.Drawing.Size(669, 194); 
      this.dataGridView1.TabIndex = 0; 
      // 
      // idDataGridViewTextBoxColumn 
      // 
      this.idDataGridViewTextBoxColumn.DataPropertyName = "Id"; 
      this.idDataGridViewTextBoxColumn.HeaderText = "Id"; 
      this.idDataGridViewTextBoxColumn.Name = "idDataGridViewTextBoxColumn"; 
      this.idDataGridViewTextBoxColumn.ReadOnly = true; 
      // 
      // itemNameDataGridViewTextBoxColumn 
      // 
      this.itemNameDataGridViewTextBoxColumn.DataPropertyName = "ItemName"; 
      this.itemNameDataGridViewTextBoxColumn.HeaderText = "ItemName"; 
      this.itemNameDataGridViewTextBoxColumn.Name = "itemNameDataGridViewTextBoxColumn"; 
      this.itemNameDataGridViewTextBoxColumn.ReadOnly = true; 
      // 
      // goldDataGridViewTextBoxColumn 
      // 
      this.goldDataGridViewTextBoxColumn.DataPropertyName = "Gold"; 
      this.goldDataGridViewTextBoxColumn.HeaderText = "Gold"; 
      this.goldDataGridViewTextBoxColumn.Name = "goldDataGridViewTextBoxColumn"; 
      this.goldDataGridViewTextBoxColumn.ReadOnly = true; 
      // 
      // silverDataGridViewTextBoxColumn 
      // 
      this.silverDataGridViewTextBoxColumn.DataPropertyName = "Silver"; 
      this.silverDataGridViewTextBoxColumn.HeaderText = "Silver"; 
      this.silverDataGridViewTextBoxColumn.Name = "silverDataGridViewTextBoxColumn"; 
      this.silverDataGridViewTextBoxColumn.ReadOnly = true; 
      // 
      // copperDataGridViewTextBoxColumn 
      // 
      this.copperDataGridViewTextBoxColumn.DataPropertyName = "Copper"; 
      this.copperDataGridViewTextBoxColumn.HeaderText = "Copper"; 
      this.copperDataGridViewTextBoxColumn.Name = "copperDataGridViewTextBoxColumn"; 
      this.copperDataGridViewTextBoxColumn.ReadOnly = true; 
      // 
      // perQtyDataGridViewTextBoxColumn 
      // 
      this.perQtyDataGridViewTextBoxColumn.DataPropertyName = "PerQty"; 
      this.perQtyDataGridViewTextBoxColumn.HeaderText = "PerQty"; 
      this.perQtyDataGridViewTextBoxColumn.Name = "perQtyDataGridViewTextBoxColumn"; 
      this.perQtyDataGridViewTextBoxColumn.ReadOnly = true; 
      // 
      // abchristDataSet 
      // 
      this.abchristDataSet.DataSetName = "abchristDataSet"; 
      this.abchristDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; 
      // 
      // itemsTableAdapter 
      // 
      this.itemsTableAdapter.ClearBeforeFill = true; 
      // 
      // itemsBindingSource1 
      // 
      this.itemsBindingSource1.DataMember = "Items"; 
      this.itemsBindingSource1.DataSource = this.abchristDataSet; 
      // 
      // AHCheckerForm 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.BackColor = System.Drawing.Color.Cornsilk; 
      this.ClientSize = new System.Drawing.Size(819, 432); 
      this.Controls.Add(this.tabControl1); 
      this.Controls.Add(this.menuStrip1); 
      this.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 
      this.MainMenuStrip = this.menuStrip1; 
      this.Name = "AHCheckerForm"; 
      this.Text = "AHPriceChecker4Free - By Cobras.Club"; 
      this.TopMost = true; 
      this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.AHCheckerForm_FormClosed); 
      this.Load += new System.EventHandler(this.AHCheckerForm_Load); 
      this.menuStrip1.ResumeLayout(false); 
      this.menuStrip1.PerformLayout(); 
      ((System.ComponentModel.ISupportInitialize)(this.dgvAuctionTable)).EndInit(); 
      this.tabControl1.ResumeLayout(false); 
      this.tabPage1.ResumeLayout(false); 
      this.tabPage1.PerformLayout(); 
      this.tabPage2.ResumeLayout(false); 
      this.tabPage2.PerformLayout(); 
      this.tabPage3.ResumeLayout(false); 
      this.tabPage3.PerformLayout(); 
      this.tabPage4.ResumeLayout(false); 
      ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); 
      ((System.ComponentModel.ISupportInitialize)(this.abchristDataSet)).EndInit(); 
      ((System.ComponentModel.ISupportInitialize)(this.itemsBindingSource1)).EndInit(); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.MenuStrip menuStrip1; 
     private System.Windows.Forms.ToolStripMenuItem tsmMain; 
     private System.Windows.Forms.ToolStripMenuItem tsmLoad; 
     private System.Windows.Forms.TextBox txtItemName; 
     private System.Windows.Forms.Button btnSearch; 
     private System.Windows.Forms.OpenFileDialog ofdOpen; 
     private System.Windows.Forms.SaveFileDialog sfdSave; 
     private System.Windows.Forms.Button btnMassSearch; 
     private System.Windows.Forms.ToolStripMenuItem tsmSave; 
     private System.Windows.Forms.DataGridViewTextBoxColumn itemPriceHistory; 
     private System.Windows.Forms.DataGridViewTextBoxColumn itemAveragePrice; 
     private System.Windows.Forms.DataGridViewTextBoxColumn itemLastPrice; 
     private System.Windows.Forms.DataGridViewTextBoxColumn itemName; 
     private System.Windows.Forms.DataGridView dgvAuctionTable; 
     private System.Windows.Forms.TabControl tabControl1; 
     private System.Windows.Forms.TabPage tabPage1; 
     private System.Windows.Forms.TabPage tabPage2; 
     private System.Windows.Forms.Label label1; 
     private System.Windows.Forms.RichTextBox rtbSave; 
     private System.Windows.Forms.TabPage tabPage3; 
     private System.Windows.Forms.Label label8; 
     private System.Windows.Forms.Label label7; 
     private System.Windows.Forms.Label label6; 
     private System.Windows.Forms.Label label5; 
     private System.Windows.Forms.Label label4; 
     private System.Windows.Forms.Label label3; 
     private System.Windows.Forms.Label label2; 
     private System.Windows.Forms.TabPage tabPage4; 
     private System.Windows.Forms.DataGridView dataGridView1; 
     private abchristDataSet abchristDataSet; 
     private abchristDataSetTableAdapters.ItemsTableAdapter itemsTableAdapter; 
     private System.Windows.Forms.DataGridViewTextBoxColumn idDataGridViewTextBoxColumn; 
     private System.Windows.Forms.DataGridViewTextBoxColumn itemNameDataGridViewTextBoxColumn; 
     private System.Windows.Forms.DataGridViewTextBoxColumn goldDataGridViewTextBoxColumn; 
     private System.Windows.Forms.DataGridViewTextBoxColumn silverDataGridViewTextBoxColumn; 
     private System.Windows.Forms.DataGridViewTextBoxColumn copperDataGridViewTextBoxColumn; 
     private System.Windows.Forms.DataGridViewTextBoxColumn perQtyDataGridViewTextBoxColumn; 
     private System.Windows.Forms.BindingSource itemsBindingSource1; 
    } 
} 
+0

提供更多背景,張貼一些代碼。你有沒有例外?轉換爲類庫之後,你在哪裏引用它?您是否嘗試調試代碼以查看發生了什麼? –

+0

.dll是與.exe相同的代碼庫,但沒有執行條目 - 「Main()」方法。所以這個項目的類型不是你的問題。也許一些初始化代碼在'Main'方法中,並且如果項目類型改變爲「Class Library」,它將不會被執行。 – Fabio

+0

我更新了一些代碼以供審查。我只能添加這麼多,因爲這個網站的字符限制。我沒有任何例外。我正在考慮爲1選項卡構建一個單獨的控制檯應用程序。我有關於datagridview的問題,所以我可以調試datagridview並將行傳遞給它。 – DSWH

回答

0

你必須在你的類庫,可以讓你設置連接字符串添加代碼。設置連接字符串的地方在你的類庫的構造函數中。您可以手動添加連接字符串這樣在App.config:

<connectionStrings> 
    <add name="MyDB" connectionString="Server=.\SQLExpress; 
     AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname; 
     Trusted_Connection=Yes;" /> 
</connectionStrings> 

然後訪問配置字符串是這樣的:

var str = ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString 
+0

謝謝你提醒我回到基礎知識並將其分解,結果問題變成了更多。網絡安全問題,我從頭開始編寫所有代碼,而不是生成任何代碼,並儘可能遠離C#代碼以外的其他任何東西,這個項目現在正在工作中,現在唯一需要解決的是當它打開時出現的美觀圖形故障。它在打開時捕獲DataGridView的背景屏幕。 – DSWH