如何在访问数据库中保存我的数据 [英] How to save my data in access database
本文介绍了如何在访问数据库中保存我的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
INSERT INTO PROBLEM at command.ExecuteNonQuery();
我尝试过:
INSERT INTO PROBLEM AT command.ExecuteNonQuery();
What I have tried:
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 MDIFORM
{
public partial class ticketing1 : Form
{
private OleDbConnection connection = new OleDbConnection();
public ticketing1()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ASSISTANT MANAGER\Downloads\2nd semester\2nd semester registrable courses\MDIFORM\busdata.accdb;
Persist Security Info=False;";
}
private void button2_Click(object sender, EventArgs e)
{
try
{
OleDbCommand command = new OleDbCommand();
command.CommandType = CommandType.Text;
command.CommandText = "INSERT INTO ticketing1(Bustype,Busnumber,Routenumber,Depot,Time,Deta,Bstop,Estop,Startplace,Endplace,Child,Adult,Half,Full,Mincharge,Totalfare)" + " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
command.Parameters.AddWithValue("Busnumber", Bustype.Text);
command.Parameters.AddWithValue("Routenumber", Routenumber.Text);
command.Parameters.AddWithValue("Depot", Depot.Text);
command.Parameters.AddWithValue("Time", Time.Text);
command.Parameters.AddWithValue("Deta", Deta.Text);
command.Parameters.AddWithValue("Bstop", Bstop.Text);
command.Parameters.AddWithValue("Estop", Estop.Text);
command.Parameters.AddWithValue("Startplace", Startplace.Text);
command.Parameters.AddWithValue("Child", Child.Text);
command.Parameters.AddWithValue("Adult", Adult.Text);
command.Parameters.AddWithValue("Half", Half.Text);
command.Parameters.AddWithValue("Full", Full.Text);
command.Parameters.AddWithValue("Mincharge", Mincharge.Text);
command.Parameters.AddWithValue("Totalfare", Totalfare.Text);
command.Connection = connection;
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
testdata.Text = "connection successful";
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
finally
{
connection.Close();
}
}
private void button3_Click(object sender, EventArgs e)
{
connection.Close();
connection.Dispose();
this.Hide();
Form1 fm = new Form1();
fm.ShowDialog();
}
private void ticketing1_Load(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "Select Bustype From Busdetails ";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Bustype.Items.Add(reader[0]).ToString();
}
testdata.Text = "connection successful";
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
finally
{
connection.Close();
}
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query1 = "Select Depot From Busdetails ";
command.CommandText = query1;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Depot.Items.Add(reader[0]).ToString();
}
testdata.Text = "connection successful";
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
finally
{
connection.Close();
}
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query2 = "Select Routenumber From ROUTE_MANAGEMENT ";
command.CommandText = query2;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Routenumber.Items.Add(reader[0]).ToString();
}
testdata.Text = "connection successful";
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
finally
{
connection.Close();
}
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query3 = "Select Busnumber From Busdetails ";
command.CommandText = query3;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Busnumber.Items.Add(reader[0]).ToString();
}
testdata.Text = "connection successful";
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
finally
{
connection.Close();
}
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query4 = "Select Bstop From ROUTE_MANAGEMENT ";
command.CommandText = query4;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Bstop.Items.Add(reader[0]).ToString();
}
testdata.Text = "connection successful";
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
finally
{
connection.Close();
}
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query5 = "Select Estop From ROUTE_MANAGEMENT ";
command.CommandText = query5;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Estop.Items.Add(reader[0]).ToString();
}
testdata.Text = "connection successful";
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
finally
{
connection.Close();
}
}
}
}
推荐答案
错误信息是。 ..什么?
我猜你作为字符串提供的一些值应该是不同的类型(例如数字,DateTime)。因此,在添加参数时转换它们,例如
And the error message is ... what?
I guess that some of the values which you provide as strings ought to be different types (e.g. numbers, DateTime). Hence convert them when you add the parameters, e.g.
command.Parameters.AddWithValue("Mincharge", Convert.ToDouble(Mincharge.Text));
这篇关于如何在访问数据库中保存我的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文