在数据库中建立了一个相应的"学生信息表",怎样连接到数据库并向表中添加信息??
看了两遍书还是不大明白
使用ado.net..
Access:
OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source =D:\\mdb\\NorthWind.mdb");
Conn.Open();
OleDbCommand Cmd = new OldDbCommand();
Cmd.CommmandType = CommandType.Text;
Cmd.CommandText = "Insert Info 学生信息表(学号,年龄,性别,专业) Values (111,18,'男','会计')";
Cmd.Connection = Conn;
Cmd.ExecuteNonQuery();
MessageBox.Show("AddNew Success");
int num = Convert.ToInt32(this.TextBox1.Text.Trim());
int age = Convert.ToInt32(this.TextBox2.Text.Trim());
string sex = this.TextBox3.Text.Trim();
string zy = this.TextBox4.Text.Trim();
string InsertStr = "Insert Info 学生信息表(学号,年龄,性别,专业) Values ("+num+","+age+",'"+sex="','"+zy+"')";
SqlConnection myconn = new SqlConnection("server=服务器;database=数据库;id=数据库用户名;pwd=数据库密码;");
myconn.Open();
SqlCommand cmd = new SqlCommand(InsertStr,myconn);
cmd.ExecuteNonQuery();
myconn.Close();