This post is for the Online Method Database (Connected Approach) in .NET Framework. Following are the three methods to do the same:
int
vEmpno;
String vEname;
int vSalary;
//insert command
//Pick all the values from the textboxes
vEmpno = Convert.ToInt32(textBox1.Text);
vEname = textBox2.Text;
vSalary = Convert.ToInt32(textBox3.Text);
String vStr1 = "insert into Emp (Empno, Ename, Salary) values (" + vEmpno + ",'" + vEname + "'," + vSalary + ")";
MessageBox.Show("Record Saved");
vConn.Close();
String vStr1 = "Create table emp(Empno numeric(10),Ename varchar(20),Salary numeric(10))";
SqlCommand vComm = new SqlCommand(vStr1, vConn);
vComm.ExecuteNonQuery();
MessageBox.Show("Table Created");
vConn.Close();
int vSalary;
vEname = textBox2.Text;
vSalary = Convert.ToInt32(textBox3.Text);
String vStr1 = "insert into Emp (Empno, Ename, Salary) values (" + vEmpno + ",'" + vEname + "'," + vSalary + ")";
MessageBox.Show("Record saved");
vConn.Close();
Best of luck with your coding. Keep on trying.
OLEDB Method
OleDbConnection
vConn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data
source=c:\\MyDatabase.mdb");
vConn.Open();
String vEname;
int vSalary;
//insert command
//Pick all the values from the textboxes
vEmpno = Convert.ToInt32(textBox1.Text);
vEname = textBox2.Text;
vSalary = Convert.ToInt32(textBox3.Text);
String vStr1 = "insert into Emp (Empno, Ename, Salary) values (" + vEmpno + ",'" + vEname + "'," + vSalary + ")";
OleDbCommand vComm = new OleDbCommand(vStr1, vConn);
vComm.ExecuteNonQuery();MessageBox.Show("Record Saved");
vConn.Close();
SQLSERVER
Methodology
SqlConnection
vConn = new SqlConnection("server=localhost; database=rohit; user id=sa;
pwd=123");
vConn.Open();
String vStr1 = "Create table emp(Empno numeric(10),Ename varchar(20),Salary numeric(10))";
SqlCommand vComm = new SqlCommand(vStr1, vConn);
vComm.ExecuteNonQuery();
MessageBox.Show("Table Created");
vConn.Close();
ODBC
Methodology
OdbcConnection vConn = new OdbcConnection("dsn=LovelyDSN;uid=sa;
pwd=123");
vConn.Open();
int
vEmpno;
String vEname;int vSalary;
//Pick
all the values from the textboxes
vEmpno = Convert.ToInt32(textBox1.Text);vEname = textBox2.Text;
vSalary = Convert.ToInt32(textBox3.Text);
String vStr1 = "insert into Emp (Empno, Ename, Salary) values (" + vEmpno + ",'" + vEname + "'," + vSalary + ")";
OdbcCommand vComm = new OdbcCommand(vStr1, vConn);
vComm.ExecuteNonQuery();MessageBox.Show("Record saved");
vConn.Close();
Best of luck with your coding. Keep on trying.
Comments