【发布时间】:2016-06-03 11:12:42
【问题描述】:
我似乎无法解决它我正在尝试连接到我的访问数据库,但我无法并不断出错。谁能帮帮我
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form2()
{
InitializeComponent();
string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
string path = (System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData("DataDirectory", path);
OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.accdb;User Id=admin; Password=;");
}
private void button4_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3(); // Instantiate a Form3 object.
f3.Show(); // Show Form3 and
this.Close(); // closes the Form2 instance
}
private void button1_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText= "INSERT into Dataaa ([FirstName],[LastName],[ICNO],[Address],[Loan],[Percent],[Payback],[StartDate],[EndDate],[Monthly],[PaymentType],[Remark]) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "')";
command.ExecuteNonQuery();
MessageBox.Show("Details have been Saved.");
}
catch (Exception ex)
{
MessageBox.Show("error " + ex);
}
finally
{
connection.Close();
}
}
【问题讨论】:
-
你能发布错误信息吗?
-
@M.Schena 连接字符串属性尚未初始化
标签: c# ms-access connection-string