C# Sql 数据不保存

发布于 2024-12-09 10:45:57 字数 3412 浏览 0 评论 0原文

我目前正在处理的 ac# 应用程序中有一些表,其中 4/5 的表一切都保存得很好,没有任何问题。对于第 5 个表,一切似乎都很好,直到我再次重新加载程序(无需修改代码或使用单独的安装,以便数据不会消失)。 4/5 表很好,但第 5 个表没有任何记录重新启动后(但它是上次运行时执行的)。下面是一些代码摘录。我在线尝试了一些不同的解决方案,包括创建一个字符串来手动在数据库上运行 sql 命令以及直接创建行,而不是下面使用通用数据行的实现。

//From main window
        private void newInvoice_Click(object sender, EventArgs e)
        {
            PosDatabaseDataSet.InvoicesRow newInvoice = posDatabaseDataSet1.Invoices.NewInvoicesRow();
            Invoices iForm = new Invoices(newInvoice, posDatabaseDataSet1, true);
        }

//Invoices Table save [Works] (from Invoices.cs)
 private void saveInvoice_Click(object sender, EventArgs e)
        {
            iRecord.Date = Convert.ToDateTime(this.dateField.Text);
            iRecord.InvoiceNo = Convert.ToInt32(this.invoiceNumField.Text);
            iRecord.Subtotal = (float) Convert.ToDouble(this.subtotalField.Text);
            iRecord.Tax1 = (float)Convert.ToDouble(this.hstField.Text);
            iRecord.Total = (float)Convert.ToDouble(this.totalField.Text);
            iRecord.BillTo = this.billToField.Text;
            invoicesBindingSource.EndEdit();
            if (newRecord)
            {
                dSet.Invoices.Rows.Add(iRecord);
                invoicesTableAdapter.Adapter.Update(dSet.Invoices);
            }
            else
            {
                string connString = Properties.Settings.Default.PosDatabaseConnectionString;
                string queryString = "UPDATE dbo.Invoices set ";
                queryString += "Date='" + iRecord.Date+"'";
                queryString += ", Subtotal=" + iRecord.Subtotal;
                queryString += ", Tax1=" + iRecord.Tax1.ToString("N2");
                queryString += ", Total=" + iRecord.Total;
                queryString += " WHERE InvoiceNo=" + iRecord.InvoiceNo;
                using (SqlConnection dbConn = new SqlConnection(connString))
                {
                    SqlCommand command = new SqlCommand(queryString, dbConn);
                    dbConn.Open();
                    SqlDataReader r = command.ExecuteReader();
                    dbConn.Close();
                }
            }
            dSet.Invoices.AcceptChanges();
        }

//Invoice Items save [works until restart] (also from Invoices.cs)
  private void addLine_Click(object sender, EventArgs e)
        {
                DataRow iRow = dSet.Tables["InvoiceItems"].NewRow();
                iRow["Cost"] = (float)Convert.ToDouble(this.costField.Text);
                iRow["Description"] = this.descriptionField.Text;
                iRow["InvoiceNo"] = Convert.ToInt32(this.invoiceNumField.Text);
                iRow["JobId"] = Convert.ToInt32(this.jobIdField.Text);
                iRow["Qty"] = Convert.ToInt32(this.quantityField.Text);
                iRow["SalesPerson"] = Convert.ToInt32(this.salesPersonField.Text);
                iRow["SKU"] = Convert.ToInt32(this.skuField.Text);

                dSet.Tables["InvoiceItems"].Rows.Add(iRow);
                invoiceItemsTableAdapter.Adapter.Update(dSet,"InvoiceItems");
                PosDatabaseDataSet.InvoiceItemsDataTable dTable = (PosDatabaseDataSet.InvoiceItemsDataTable)dSet.InvoiceItems.Copy();
                DataRow[] d = dTable.Select("InvoiceNo=" + invNo.ToString());
                invoiceItemsView.DataSource = d;

        }

预先感谢您的任何见解。

更新:2011 年 10 月 17 日。我仍然无法让这个工作还有更多的想法吗?

I have a few tables in a c# application I'm currently working on and for 4/5 of the tables everything saves perfectly fine no issues. For the 5th table everything seems good until I reload the program again (without modifying the code or working with a seperate install so that the data doesn't go away) The 4/5 tables are fine but the 5th doesn't have any records in it after it has been restarted (but it did the last time it was running). Below is some code excerpts. I have tried a few different solutions online including creating a string to run the sql commands on the database manually and creating the row directly as opposed to the below implementation which uses a generic data row.

//From main window
        private void newInvoice_Click(object sender, EventArgs e)
        {
            PosDatabaseDataSet.InvoicesRow newInvoice = posDatabaseDataSet1.Invoices.NewInvoicesRow();
            Invoices iForm = new Invoices(newInvoice, posDatabaseDataSet1, true);
        }

//Invoices Table save [Works] (from Invoices.cs)
 private void saveInvoice_Click(object sender, EventArgs e)
        {
            iRecord.Date = Convert.ToDateTime(this.dateField.Text);
            iRecord.InvoiceNo = Convert.ToInt32(this.invoiceNumField.Text);
            iRecord.Subtotal = (float) Convert.ToDouble(this.subtotalField.Text);
            iRecord.Tax1 = (float)Convert.ToDouble(this.hstField.Text);
            iRecord.Total = (float)Convert.ToDouble(this.totalField.Text);
            iRecord.BillTo = this.billToField.Text;
            invoicesBindingSource.EndEdit();
            if (newRecord)
            {
                dSet.Invoices.Rows.Add(iRecord);
                invoicesTableAdapter.Adapter.Update(dSet.Invoices);
            }
            else
            {
                string connString = Properties.Settings.Default.PosDatabaseConnectionString;
                string queryString = "UPDATE dbo.Invoices set ";
                queryString += "Date='" + iRecord.Date+"'";
                queryString += ", Subtotal=" + iRecord.Subtotal;
                queryString += ", Tax1=" + iRecord.Tax1.ToString("N2");
                queryString += ", Total=" + iRecord.Total;
                queryString += " WHERE InvoiceNo=" + iRecord.InvoiceNo;
                using (SqlConnection dbConn = new SqlConnection(connString))
                {
                    SqlCommand command = new SqlCommand(queryString, dbConn);
                    dbConn.Open();
                    SqlDataReader r = command.ExecuteReader();
                    dbConn.Close();
                }
            }
            dSet.Invoices.AcceptChanges();
        }

//Invoice Items save [works until restart] (also from Invoices.cs)
  private void addLine_Click(object sender, EventArgs e)
        {
                DataRow iRow = dSet.Tables["InvoiceItems"].NewRow();
                iRow["Cost"] = (float)Convert.ToDouble(this.costField.Text);
                iRow["Description"] = this.descriptionField.Text;
                iRow["InvoiceNo"] = Convert.ToInt32(this.invoiceNumField.Text);
                iRow["JobId"] = Convert.ToInt32(this.jobIdField.Text);
                iRow["Qty"] = Convert.ToInt32(this.quantityField.Text);
                iRow["SalesPerson"] = Convert.ToInt32(this.salesPersonField.Text);
                iRow["SKU"] = Convert.ToInt32(this.skuField.Text);

                dSet.Tables["InvoiceItems"].Rows.Add(iRow);
                invoiceItemsTableAdapter.Adapter.Update(dSet,"InvoiceItems");
                PosDatabaseDataSet.InvoiceItemsDataTable dTable = (PosDatabaseDataSet.InvoiceItemsDataTable)dSet.InvoiceItems.Copy();
                DataRow[] d = dTable.Select("InvoiceNo=" + invNo.ToString());
                invoiceItemsView.DataSource = d;

        }

Thanks in advance for any insight.

UPDATE: October 17, 2011. I am still unable to get this working is there any more ideas out there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铃予 2024-12-16 10:45:57

您必须执行 Sql 命令才能保留所做的更改。

using (SqlConnection dbConn = new SqlConnection(connString))
{
  dbConn.Open();
  SqlCommand command = new SqlCommand(queryString, dbConn);
  command.ExecuteNonQuery();
  dbConn.Close();
}

ExecuteReader 方法的目的是(正如其名称所示)从 SQL 表中读取数据。您需要使用不同的方法,如上面所示。

you must execute your Sql Command in order to persis the changes you made.

using (SqlConnection dbConn = new SqlConnection(connString))
{
  dbConn.Open();
  SqlCommand command = new SqlCommand(queryString, dbConn);
  command.ExecuteNonQuery();
  dbConn.Close();
}

The ExecuteReader method is intended (as the name says) to read the data from a SQL table. You need to use a different method as you can see above.

深爱成瘾 2024-12-16 10:45:57

我们首先需要更多信息,您还没有显示代码失败的情况。

此类代码的常见错误是在实际将更改提交到数据库之前调用DataSet.AcceptChanges()

其次是通过绑定源进行数据绑定数据与直接编辑数据集之间的冲突。

让我们看看适当的代码,我们可以尝试提供帮助。

在调用invoiceItemsTableAdapter 后设置断点,并检查InvoiceItems 表中已添加的行。释放断点,然后关闭您的应用程序。再次检查数据库。我想说另一个表可能会强制覆盖发票项目表。

We need some more info first, you haven't shown the case where your code fails.

Common mistakes on this kind of code is calling DataSet.AcceptChanges() before actually committing the changes to the database.

Second is a conflict between databound data through the binding source vs edits to the dataset directly.

Lets see the appropriate code and we can try and help.

Set a breakpoint after teh call to invoiceItemsTableAdapter and check the InvoiceItems table for the row you have added. Release the breakpoint and then close your app. Check the database again. I would say that another table may be forcibly overwriting the invoice item table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文