Anda di halaman 1dari 8

Assignment-1

Name: Hardik Mistry


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace Assignment2 { publicclassassign { SqlConnection con; SqlDataAdapter adp; SqlCommandBuilder build; DataSet ds;

Advance .net Tech


Enrollment No:080070107026

public assign() { con = newSqlConnection(); con.ConnectionString = @"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\Hardik\Desktop\northwnd.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True"; con.Open(); } publicvoid display(String t1) { ds = newDataSet(); adp = newSqlDataAdapter("Select * from " + t1, con); build = newSqlCommandBuilder(adp); adp.Fill(ds); int i = ds.Tables[0].Columns.Count; //no of colum in paricular table int r = ds.Tables[0].Rows.Count; // no of rows in paricular table /*foreach (DataRow dr in ds.Tables[0].Rows) { Console.Write(dr[0] + "\t"); Console.Write(dr[1] + "\t"); Console.Write(dr[2] + "\t"); Console.Write(dr[3] + "\t"); Console.WriteLine();

}*/ for (int k = 0; k < i; k++) //printing column name { Console.Write(ds.Tables[0].Columns[k].ColumnName+"\t"); } Console.WriteLine(); foreach (DataRow dr in ds.Tables[0].Rows) { for (int j = 0; j < i; j++) { if (!(ds.Tables[0].Columns[j].DataType.ToString()).Equals("System.Byte[]")) //skipping byte[] array { Console.Write(dr[j] + "\t"); } } Console.WriteLine(); } Console.WriteLine("No of Colums in" + " " + t1 + " " + "are:" + " " + i); Console.WriteLine("No of Rows in" + " " + t1 + " " + "are:" + " " + r); Console.WriteLine(); } publicvoid insert(String t2) { ds = newDataSet(); SqlParameter []para; SqlDataAdapter adp1 = newSqlDataAdapter("Select * from " + t2, con); SqlCommandBuilder build1 = newSqlCommandBuilder(adp1); SqlCommand sqlquery; try { //con.Open(); adp1.Fill(ds); int i = ds.Tables[0].Columns.Count; String[] s = newString[200]; int j = 0; string query2,query1; foreach (DataColumn cols in ds.Tables[0].Columns) { Console.Write(s[j] = cols.ColumnName); Console.Write("\t" + ds.Tables[0].Columns[j].DataType); Console.WriteLine(); j++; }

query2 ="@"+s[0]; query1 = s[0]; for (j = 0; j < i-1; j++) { query1 +=","+s[j+1]; } for (j = 0; j < i-1; j++) { query2 += ","+"@" + s[j+1]; } Console.WriteLine(); string query = "INSERT INTO " + t2 + " " + "(" + query1 + ")" + " " + "VALUES" +" "+ "(" + query2 + ")"; Console.WriteLine(query); Console.WriteLine(); sqlquery = newSqlCommand(query, con); SqlCommand sqlquery1 =newSqlCommand("SET IDENTITY_INSERT"+" "+ t2+" "+ "ON",con); sqlquery1.ExecuteNonQuery(); para = newSqlParameter[i]; int h=0; Console.WriteLine("ADD values:"); for ( j = 0 ; j < i ; j++) { para[j] = newSqlParameter(@s[j],ds.Tables[0].Columns[j].DataType); sqlquery.Parameters.Add(para[j]); Console.Write((j+1)+ "." + s[j] + ":"); para[j].Value = Convert.ChangeType(Console.ReadLine(), ds.Tables[0].Columns[j].DataType); Console.WriteLine(); } h = sqlquery.ExecuteNonQuery(); if (h == 1) { Console.WriteLine(h+" "+"Rows added SuccessFully!!"); } else { Console.WriteLine("Rows is not added!!!"); }

Console.WriteLine(); } catch (Exception e) { Console.WriteLine("OOoopss!!Sql Errror" + e.ToString()); } //con.Close(); SqlCommand sqlquery2 =newSqlCommand("SET IDENTITY_INSERT"+" "+ t2+" "+ "OFF",con); sqlquery2.ExecuteNonQuery(); } publicvoid delete(String t2) { ds = newDataSet(); SqlDataAdapter adp1 = newSqlDataAdapter("Select * from " + t2, con); SqlCommandBuilder build1 = newSqlCommandBuilder(adp1); adp1.Fill(ds); int i = ds.Tables[0].Columns.Count; int n=1,j=0; String[] s = newString[200]; SqlParameter para = newSqlParameter();

foreach (DataColumn cols in ds.Tables[0].Columns) { s[j] = cols.ColumnName; Console.Write(n + "." + s[j]); n++; j++; Console.WriteLine(); } display(t2); n = 0; Console.WriteLine("In Which Column You Want To Delete Data:"); n = Int32.Parse(Console.ReadLine()); SqlCommand sqlquery; String query = "DELETE FROM" + " " + t2 + " " + "WHERE" + " " + s[n - 1] + "=" + "@" + s[n - 1]; sqlquery = newSqlCommand(query, con); Console.WriteLine(query); para = newSqlParameter(@s[n - 1], ds.Tables[0].Columns[n - 1].DataType); sqlquery.Parameters.Add(para);

Console.WriteLine("ADD value for" + " " + s[n - 1] + ":"); para.Value = Convert.ChangeType(Console.ReadLine(), ds.Tables[0].Columns[n 1].DataType); int rowaffect = sqlquery.ExecuteNonQuery(); if (rowaffect != 0) { Console.WriteLine(rowaffect + "Row(s) Deleted SuccessFully!!"); } else { Console.WriteLine("Deletion Operation failed:"); } } publicvoid update(string t1) { display(t1); ds = newDataSet(); SqlDataAdapter adp1 = newSqlDataAdapter("Select * from " + t1, con); SqlCommandBuilder build1 = newSqlCommandBuilder(adp1); adp1.Fill(ds); int j = 0; int[] s1 = newint[10]; SqlCommand sqlquery; String[] s = newstring[100]; SqlParameter []para_set = newSqlParameter[50]; SqlParameter para_wh = newSqlParameter(); String []setpara= newString[10]; //parameters for set clause in query foreach (DataColumn cols in ds.Tables[0].Columns) { s[j] = cols.ColumnName; Console.Write((j + 1) + "." + s[j] + "\t"); j++; } Console.WriteLine(); Console.WriteLine("How many Column you want to use in set clause?->"); int set_no_column = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter Column Number For those Column:"); int c; for(c=0 ; c < set_no_column ; c++) { Console.WriteLine("Column no:"+(c+1));

int n = Int32.Parse(Console.ReadLine()); setpara[c]=s[n-1]; s1[c] = n - 1; } Console.WriteLine("Which column you want use in where Clause?->"); int wh_column = Int32.Parse(Console.ReadLine()); int c1 = 0; String query = "UPDATE" + " " + t1 + " " + "SET" + " "; for(c1 = 0; c1 < set_no_column-1; c1++) { query += setpara[c1] + "=" + "@" + setpara[c1] + ","; } query+= setpara[c1] + "=" + "@" + setpara[c1]; query+=" "+"WHERE"+" "+s[wh_column-1]+"="+"@"+s[wh_column-1]; sqlquery = newSqlCommand(query, con); Console.WriteLine(query); //set clause parameter int l = 0; Console.WriteLine("Set the values of parameters:"); for (j = 0; j < set_no_column; j++) { para_set[j] = newSqlParameter(@setpara[j], ds.Tables[0].Columns[s1[l]].DataType); sqlquery.Parameters.Add(para_set[j]); Console.Write(setpara[j]+":"); para_set[j].Value = Convert.ChangeType(Console.ReadLine(), ds.Tables[0].Columns[s1[l]].DataType); Console.WriteLine(); l++; } //where clause parameter para_wh = newSqlParameter(@s[wh_column - 1], SqlDbType.Variant); sqlquery.Parameters.Add(para_wh); Console.WriteLine("Where Caluse:"); Console.WriteLine(s[wh_column-1] + ":"); para_wh.Value = Convert.ChangeType(Console.ReadLine(), ds.Tables[0].Columns[wh_column-1].DataType); int rowaffect = sqlquery.ExecuteNonQuery();

if (rowaffect != 0) { Console.WriteLine(rowaffect + "Row(s)Updated SuccessFully!!"); } else { Console.WriteLine("Updation Operation failed:"); } display(t1); }

publicString menu() { Console.WriteLine("1. For Display"); Console.WriteLine("2. For Insert"); Console.WriteLine("3. For Update"); Console.WriteLine("4. For Delete"); Console.WriteLine("5. q for quit"); Console.WriteLine(); Console.Write("Enter Chioce:"); String str = "h"; str = Console.ReadLine(); return str; }

staticvoid Main() { string s; assign a = newassign(); String table; s = a.menu(); while (s.Equals("1") | s.Equals("2") | s.Equals("3") | s.Equals("4")) //while (!(s.Equals("q")) ) { Console.WriteLine(); Console.Write("Enter Table name:"); table = Console.ReadLine(); if (s.Equals("1")) { a.display(table); Console.WriteLine(); //Display

s = a.menu(); } elseif (s.Equals("2")) //Insert { //assign a = new assign(); a.insert(table); Console.WriteLine(); s = a.menu(); } elseif(s.Equals("3")) //update the table { a.update(table); Console.WriteLine(); s=a.menu(); } elseif (s.Equals("4")) //delete the record { a.delete(table); Console.WriteLine(); s = a.menu(); } } } } }