将数据保存到数据库(单表)C#

时间:2014-04-15 15:11:23

标签: c# database visual-studio-2010 ms-access save

我一直关注此处的文档:

http://msdn.microsoft.com/en-us/library/0f92s97z%28v=vs.100%29.aspx

但是,我无法让我的申请工作!

请参阅我的简短视频: https://www.youtube.com/watch?v=zpOCaT6NaGs

我的应用程序无法正常工作,我正在做恶梦,试图让它正常运行。

我想制作一个股票管理系统'

要求:

  1. 浏览产品(名称,价格)
  2. 将产品添加到表中
  3. 编辑现有产品
  4. 删除现有产品
  5. 保存对表的更改
  6. 创建一个订单表单,该表单允许列出所选产品并提供所选数量并显示成本
  7. 打印结果。
  8. 我似乎无法将数据保存到数据库表而没有问题。

    我的系统:

    Windows 7 - SP1 Visual Studio 2010 Professional 使用Microsoft Access数据库(DataSet) 1表连接 3个字段:" ID,proName,proPrice"

    非常欢迎任何帮助,谢谢。

    我的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;
    
    namespace KanadaStockControl
    {
        public partial class frmProducts : Form
        {
            stockDataSet dsoc;
            OleDbConnection conn;
            OleDbDataAdapter da;
    
            public frmProducts()
            {
                InitializeComponent();
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                ////Adding the items to the database
                //dboKanadaDataSetProducts.ProductsRow newProductsRow;
                //newProductsRow = dboKanadaDataSetProducts.Products.NewProductsRow();
                ////Setting the Product Name
                //newProductsRow.ProductName = txtItem.Text;
                ////Setting the Product Price
                //newProductsRow.ListPrice = txtPrice.Text;
                ////Adding the new row to the table
                //this.dboKanadaDataSetProducts.Products.Rows.Add(newProductsRow);
                ////Saving the new row to the database
                //this.productsTableAdapter.Update(this.dboKanadaDataSetProducts.Products);            
                //Old NON WORKING DATABASE ADD CODE:
                //dboKanadaDataSetProductsTableAdapters.ProductsTableAdapter productsTableAdapter = new dboKanadaDataSetProductsTableAdapters.ProductsTableAdapter();
                //productsTableAdapter.Insert("9", "NA", "0", txtPrice.Text, "Software");
    
                //lstPrice.Items.Add(txtPrice.Text);
                //lstItem.Items.Add(txtItem.Text);
                stockDataSetTableAdapters.stockTableAdapter stockTableAdaptor = new stockDataSetTableAdapters.stockTableAdapter();
                stockTableAdapter.Insert(txtItem.Text, txtPrice.Text);
            }
    
            //private void lstItem_SelectedIndexChanged(object sender, EventArgs e)
            //{
            //    int x = lstItem.SelectedIndex;
            //    lstPrice.SelectedIndices.Add(x);
            //}
    
            //private void lstPrice_SelectedIndexChanged(object sender, EventArgs e)
            //{
            //    int x = lstPrice.SelectedIndex;
            //    lstItem.SelectedIndices.Add(x);
            //}
    
            private void btnRemove_Click(object sender, EventArgs e)
            {
                if(1==1/*lstItem.SelectedItem == null*/)
                {
                    MessageBox.Show("You must select an Item first, Before you are able to delete it!");
                }
                else
                {
    
    
                    //Finding the row to delete in the database
                    //dboKanadaDataSetProducts.ProductsRow delProductsRow;
                    //delProductsRow = dboKanadaDataSetProducts.Products.FindByProductsID(string ProductName);
    
                    //START -------
                    // Locate the row to delete.
                    //NorthwindDataSet.RegionRow oldRegionRow;
                    //oldRegionRow = northwindDataSet.Region.FindByRegionID(5);
    
                    // Delete the row from the dataset
                    //oldRegionRow.Delete();
    
                    // Delete the row from the database 
                    //this.regionTableAdapter.Update(this.northwindDataSet.Region);
                    //END -------
    
                    //lstPrice.Items.Remove(lstPrice.SelectedItem);
                    //lstItem.Items.Remove(lstItem.SelectedItem);
                }
            }
    //I cannot be bothered to add spaces to this code, hence the misaligned code.
        private void btnClear_Click(object sender, EventArgs e)
        {
            //lstItem.Items.Clear();
            //lstPrice.Items.Clear();
        }
    
        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            //string context = this.txtPrice.Text;
            //for (int i = 0; i < context.Length; i++)
            //{
            //    if (!char.IsNumber(context[i]))
            //    {
            //        MessageBox.Show("Only numbers are allowed in this field!");
            //        txtPrice.Text = txtPrice.Text.Remove(txtPrice.TextLength - 1);
            //    }
            //}
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            /*
            if (MessageBox.Show("Are you sure that you want to DELETE ALL LISTED ITEMS?", "WARNING: Delete All Records",
         MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
         == DialogResult.Yes)
            {
                int j = lstPrice.Items.Count;
                for (int i = lstItem.Items.Count; i > 0; )
                {
    
                    lstItem.Items.Remove(lstItem.Items[--i]);
                    lstPrice.Items.Remove(lstPrice.Items[--j]);
                }
            }*/
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmOrder makeOrder = new frmOrder();
            makeOrder.Show();
        }
    
        //private void frmProducts_Load(object sender, EventArgs e)
        //{
        //    // TODO: This line of code loads data into the 'dboKanadaDataSetProducts.Products' table. You can move, or remove it, as needed.
        //    //this.productsTableAdapter.Fill(this.dboKanadaDataSetProducts.Products);
    
        //}
    
        private void frmProducts_Load_1(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'stockDataSet.stock' table. You can move, or remove it, as needed.
            //this.stockTableAdapter.Fill(this.stockDataSet.stock);
            dsoc = new stockDataSet();
            conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\stock.accdb");
            da = new OleDbDataAdapter("Select * from stock", conn);
            OleDbCommandBuilder cmdBl = new OleDbCommandBuilder(da);
            da.Fill(dsoc, "stock");
            dataGridView2.DataSource = dsoc;
            dataGridView2.DataMember = "stock";
        }
    
    
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //this.Validate();
                //this.stockBindingSource.EndEdit();
                //this.stockTableAdapter.Update(this.stockDataSet.stock);
                //MessageBox.Show("Update successful");
                //da.Update(stockDataSet.Tables["stock"]);
    
                System.Data.OleDb.OleDbConnection oledb1 = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\stock.accdb");
                System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "INSERT stock (proName, proPrice) VALUES ("+ txtItem.Text +", " + txtPrice.Text + ")";
                cmd.Connection = oledb1;
    
                oledb1.Open();
                cmd.ExecuteNonQuery();
                oledb1.Close();
    
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Update failed");
            }
    
    
    
            //this.dataGridView1.EndEdit();
        }
    
        private void btnMenu_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmMenu showMenu = new frmMenu();
            showMenu.Show();
        }
    }
    }
    

2 个答案:

答案 0 :(得分:0)

cmd.CommandText =“INSERT INTO stock(proName,proPrice)VALUES(”+ txtItem.Text +“,”+ txtPrice.Text +“)”;

答案 1 :(得分:-2)

它会起作用。但数据将保存为bin/debug文件夹中的数据库副本。

转到解决方案资源管理器并更改数据库的属性。设置"Copy to output Directory=Copy if newer",然后尝试。祝你好运。