我一直在研究这个问题。我正在尝试通过自定义程序向访问数据库添加值。我没有任何语法错误或任何我可以看错的东西,但我是一个新手。任何帮助将非常感激。 我有一些脏代码,我正在搞乱,但评论出来。请帮忙!谢谢你们和男孩们。
我正在使用AMD Win 10 Visual Studio 2015 访问2016
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmNewClient : Form
{
OleDbConnection connect = new OleDbConnection();
public frmNewClient()
{
InitializeComponent();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void cmdClientInput_Click(object sender, EventArgs e)
{
string CompanyName = txtCCOMPANYNAME.Text;
string ContactName = txtCCONTACTNAME.Text;
string Street = txtCSTREET.Text;
string City = txtCCITY.Text;
string State = txtCSTATE.Text;
string ZIP = txtCZIP.Text;
string Phone1 = txtCPHONE1.Text;
string Phone2 = txtCPHONE2.Text;
string Email = txtCEMAIL.Text;
string Notes = txtCNOTES.Text;
connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\lafiammadb.accdb;Persist Security Info=False";
OleDbCommand cmd = new OleDbCommand("INSERT Into Clients(CompanyName, ContactName, Street, City, State, ZIP, Phone1, Phone2, Email, Notes)" + "values(@CompanyName, @ContactName, @Street, @City, @State, @ZIP, @Phone1, @Phone2, @Email, @Notes)", connect);
if (connect.State == ConnectionState.Open)
{
cmd.Parameters.Add("@CompanyName", OleDbType.Char, 20).Value = CompanyName;
cmd.Parameters.Add("@ContactName", OleDbType.Char, 20).Value = ContactName;
cmd.Parameters.Add("@Street", OleDbType.Char, 20).Value = Street;
cmd.Parameters.Add("@City", OleDbType.Char, 20).Value = City;
cmd.Parameters.Add("@State", OleDbType.Char, 20).Value = State;
cmd.Parameters.Add("@ZIP", OleDbType.Char, 20).Value = ZIP;
cmd.Parameters.Add("@Phone1", OleDbType.Char, 20).Value = Phone1;
cmd.Parameters.Add("@Phone2", OleDbType.Char, 20).Value = Phone2;
cmd.Parameters.Add("@Email", OleDbType.Char, 20).Value = Email;
cmd.Parameters.Add("@Notes", OleDbType.Char, 20).Value = Notes;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Data Added To Database");
txtCCOMPANYNAME.Text = "";
txtCCONTACTNAME.Text = "";
txtCSTREET.Text = "";
txtCCITY.Text = "";
txtCSTATE.Text = "";
txtCZIP.Text = "";
txtCPHONE1.Text = "";
txtCPHONE2.Text = "";
txtCEMAIL.Text = "";
txtCNOTES.Text = "";
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
connect.Close();
}
}
else
{
MessageBox.Show("Connection Failed");
}
//connect.Open();
//OleDbCommand cmd = new OleDbCommand(q);
//cmd.Parameters.AddWithValue("@CompanyName", CompanyName);
//cmd.Parameters.AddWithValue("@ContactName", ContactName);
//cmd.Parameters.AddWithValue("@Street", Street);
//cmd.Parameters.AddWithValue("@City", City);
//cmd.Parameters.AddWithValue("@ZIP", ZIP);
//cmd.Parameters.AddWithValue("@Phone1", Phone1);
//cmd.Parameters.AddWithValue("@Phone2", Phone2);
//cmd.Parameters.AddWithValue("@Email", Email);
//cmd.Parameters.AddWithValue("@Notes", Notes);
//cmd.ExecuteNonQuery();
//OleDbCommand CmdSql = new OleDbCommand("Insert into [clients](Company Name, Contact Name, Street, City, State, Zip, Phone1, Phone2, Email, Notes)
}
}
}
答案 0 :(得分:2)
问题在于:
if (connect.State == ConnectionState.Open)
您正在检查连接是否已打开,但实际上您并未打开连接。在检查之前,您需要Open
您的连接。你可以尝试这样:
if (connect.State == ConnectionState.Closed)
{
connect.Open();
}
旁注:
在处理连接时,您可以考虑使用using
statament。
答案 1 :(得分:0)
这一行给出了问题if (connect.State == ConnectionState.Open)
。你真正想要做的是检查连接是否已关闭,然后打开它:
if (connect.State == ConnectionState.Closed)
{
connect.Open();
//Do Some work
}
但是我建议您使用using
块来保证对象的正确处理。
来自 MSDN
要确保始终关闭连接,请打开using块内部的连接,如以下代码片段所示。这样做可确保在代码退出块时自动关闭连接。
using(OleDbConnection connect = new OleDbConnection(connectionString))
{
connect.Open();
//Do some work
}// Here it will automatically call Dispose();
由于OleDbConnection类实现了IDisposable。因此,您可以直接使用Dispose()
方法,并且不会被要求致电connect.Close();