当进入有两个数据库的第二个表单时,会出现此错误,是我的连接问题还是其他问题? enter image description here
public partial class MenClothing : Form
{
OleDbConnection connect1 = new OleDbConnection();
public MenClothing(string text)
{
InitializeComponent();
txtUsername.Text = text;
}
public MenClothing()
{
InitializeComponent();
connect1.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Student\Downloads\OFFICAL STAC\OFFICAL STAC\StacProductions\DatabaseSaveItem.accdb";
}
private int upperCase(string pass)
{
int num = 0;
foreach (char ch in pass)
{
if (char.IsUpper(ch))
{
num++;
}
}
return num;
}
private void btnlogout_Click(object sender, EventArgs e)
{
this.Hide();
Form2 Return = new Form2(txtUsername.Text);
Return.ShowDialog();
}
private void MenClothing_Load(object sender, EventArgs e)
{
try
{
connect1.Open();
connect1.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.ImageLocation = ItemUrl.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
pictureBox1.ImageLocation = ItemUrl.Text;
}
private void button1_Click(object sender, EventArgs e)
{
{
try
{
connect1.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect1;
command.CommandText = "insert into RegisterItem([Name],[Url],[Description], [Price]) values('" + ItemName.Text + "','" + ItemUrl.Text + "','" + ItemDescription.Text + "','" + ItemPrice.Text + "')";
command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
connect1.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
connect1.Close();
}
string str = ItemUrl.Text;
pictureBox1.ImageLocation = str;
//string str = textBox1.Text;
// Image img = Image.FromFile(str);
// pictureBox1.Image = img;
txtUsername = txtID1;
ItemName = txtName1;
ItemDescription = txtDescription1;
ItemPrice = txtPrice1;
ItemName.Text = "";
ItemDescription.Text = "";
ItemPrice.Text = "";
}
}
private void label5_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (txtUsername.Text == txtID1.Text)
{
}
}
}
}
答案 0 :(得分:1)
在黑暗中只是一个刺,但如果使用文本重载调用构造函数,则不会初始化连接字符串。只有在使用默认构造函数时才会初始化它。