C# 将数据保存到数据库(单个表)C
我一直遵循以下文件: 然而,我不能让我的申请工作 请看我的短片: 我的应用程序不工作,我正在做噩梦试图让它工作 我想做一个“库存管理系统” 要求: 浏览产品名称、价格 将产品添加到表中 编辑现有产品 删除现有产品 将更改保存到表中 创建一个订单,允许列出所选产品,并提供所选数量和显示的成本 打印结果。 我似乎无法毫无问题地将数据保存到数据库表中 我的系统: Windows7-SP1 VisualStudio2010专业版 使用Microsoft Access数据库数据集 1个要连接的表 3个字段:ID、proName、proPrice 欢迎任何帮助,谢谢 我的代码:C# 将数据保存到数据库(单个表)C,c#,database,visual-studio-2010,ms-access,save,C#,Database,Visual Studio 2010,Ms Access,Save,我一直遵循以下文件: 然而,我不能让我的申请工作 请看我的短片: 我的应用程序不工作,我正在做噩梦试图让它工作 我想做一个“库存管理系统” 要求: 浏览产品名称、价格 将产品添加到表中 编辑现有产品 删除现有产品 将更改保存到表中 创建一个订单,允许列出所选产品,并提供所选数量和显示的成本 打印结果。 我似乎无法毫无问题地将数据保存到数据库表中 我的系统: Windows7-SP1 VisualStudio2010专业版 使用Microsoft Access数据库数据集 1个要连接的表 3个字段
cmd.CommandText=插入到股票proName中,属性值+txtItem.Text+,+txtPrice.Text+ 它会起作用的。但数据将保存为bin/debug文件夹中的数据库副本
转到解决方案资源管理器并更改数据库的属性。将Copy设置为output Directory=Copy(如果较新),然后重试。祝你好运。你必须告诉我们具体问题。它不起作用,问题对那些想帮助你的人没有帮助。请包括您收到的具体错误。观看原始帖子中提供的youtube视频我只想知道如何将access数据库与visual studio 2010集成,以便我可以按照上面的数字步骤进行操作。我没有收到任何错误,但是,由于对C的了解不足,我没有收到任何结果,我将添加代码…我认为您需要将内部字符串引号替换为单数引号,或者转义字符串中的双引号。否则该函数将无法正确执行?
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();
}
}
}