namespace DB1
{
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
String query="INSERT INTO Student
VALUES("+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"',
'"+textBox4.Text+")";
OleDbConnection conn = new
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\\Database11.accdb");
OleDbCommand cmd = new OleDbCommand(query,conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("data stored succesfully");
conn.Close();
textBox1.Text = (Convert.ToInt32(textBox1.Text) + 1).ToString();
textBox2.Text = (Convert.ToInt32(textBox2.Text) + 1).ToString();
textBox3.Text = (Convert.ToInt32(textBox3.Text) + 1).ToString();
textBox4.Text = (Convert.ToInt32(textBox4.Text) + 1).ToString();
textBox1.Focus();
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
autonum();
}
private void autonum()
{
string query;
query = "Select max(Stdno) from student";
OleDbConnection conn = new
OleDbConnection("Provider=Micrsoft.ACE.OLEDB.12.0;
Data Source=datasource");
OleDbCommand cmd = new OleDbCommand(query,conn);
conn.Open();
try
{
OleDbDataReader dr =cmd.ExecuteReader();
if(dr.Read())
{
textBox1.Text = (Convert.ToInt32(dr[0])+1).ToString();
}
else
{
textBox1.Text="1001";
}
dr.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
textBox1.Text = "1001";
}
conn.Close();
}
}
}
错误是:
oledb 异常未处理 查询表达式“2”、“wse”、“22”、“assd”中的语法错误(缺少运算符)。