1
我是新來的C#和「連接字符串屬性尚未初始化」是我的問題。我搜索並嘗試了我在互聯網上看到的所有內容,但它根本無法幫助我。我不知道該怎麼辦了,請幫助:(connectionstring屬性尚未初始化;標準表達式中的數據類型不匹配;數據庫不更新
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 salon
{
public partial class Form4 : Form
{
OleDbConnection conn = new OleDbConnection();
public Form4()
{
InitializeComponent();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "/myDB.accdb";
}
private void Form4_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'myDBDataSet.tblCustomerInfo' table. You can move, or remove it, as needed.
this.tblCustomerInfoTableAdapter.Fill(this.myDBDataSet.tblCustomerInfo);
}
private void btnContinue_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"insert into tblCustomerInfo(CustomerName, PhoneNo, Email) values ('" + txtName.Text + "','" + txtNo.Text + "','" + txtEmail.Text + "'";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.Close();
string date = System.DateTime.Today.ToString("ddmmyy");
MessageBox.Show("Your Information: " + Environment.NewLine + "Name: " + txtName.Text + Environment.NewLine + "Phone no.: " + txtNo.Text + Environment.NewLine + "Email: " + txtEmail.Text + Environment.NewLine + Environment.NewLine + "Your chosen date is: " + timePicker.Value);
MessageBox.Show("Your date is successfully reserved");
Form1 frm1 = new Form1();
frm1.Show();
this.Hide();
}
}
}
請幫我這一次,我是新來的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 salon
{
public partial class Form4 : Form
{
OleDbConnection conn = new OleDbConnection();
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'myDBDataSet.tblCustomerInfo' table. You can move, or remove it, as needed.
this.tblCustomerInfoTableAdapter.Fill(this.myDBDataSet.tblCustomerInfo);
}
private void btnContinue_Click(object sender, EventArgs e)
{
string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "/myDB.accdb";
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = conString;
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"insert into tblCustomerInfo(CustomerName, PhoneNo, Email) values ('" + txtName.Text + "','" + txtNo.Text + "','" + txtEmail.Text + "')";
cmd.Parameters.AddWithValue("@CustomerName", txtName.Text);
cmd.Parameters.AddWithValue("@PhoneNo", txtNo.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Your Information: " + Environment.NewLine + "Name: " + txtName.Text + Environment.NewLine + "Phone no.: " + txtNo.Text + Environment.NewLine + "Email: " + txtEmail.Text + Environment.NewLine + Environment.NewLine + "Your chosen date is: " + timePicker.Value);
MessageBox.Show("Your date is successfully reserved");
Form1 frm1 = new Form1();
frm1.Show();
this.Hide();
}
}
}
UPDATE 我定有關的錯誤數據類型不匹配但現在當我開始調試它的數據庫沒有更新有什麼問題?請幫助
謝謝,但現在我進入另一個錯誤「INSERT INTO語句中的語法錯誤。」 – Tsuna
,現在是「標準表達式中的數據類型不匹配」。它指向cmd.ExecuteNonQuery(); – Tsuna
對不起,延長的問題,雖然 – Tsuna