无法查看数据

本文关键字:数据 | 更新日期: 2023-09-27 18:08:46

我已经阅读了许多教程和文章,但仍然无法找到我需要的帮助我解决我的问题。

我的问题是,我编译的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;
    }
}

无法查看数据

您必须在类库中添加允许您设置连接字符串的代码。设置连接字符串的位置是在类库的构造函数中。你可以像这样在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