I am new to SQL and I am using Windows forms and C#.
The following 2 SQL queries work fine with no issues but I am concerned that one of them might cause problems later and I do not know which one is safer to use.
I just want to know what is the difference between the two command parameters deceleration and value assigning in both first and second methods:
The first command parameters deceleration method:
SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlCommand MyCommand = new SqlCommand();
DataTable DataTable = new DataTable();
SqlDataAdapter Sql_Data_Adapter = new SqlDataAdapter();
long PhoneNumber = 07429114523;
MyConnection.Open();
MyCommand.CommandText = "SELECT * FROM List_of_All_Orders WHERE Phone= @PhoneNumber ";
MyCommand.Connection = MyConnection;
// command parameters declaration and value assigning
MyCommand.Parameters.Add("@PhoneNumber", SqlDbType.BigInt).Value = PhoneNumber;
Sql_Data_Adapter.SelectCommand = MyCommand;
Sql_Data_Adapter.Fill(DataTable);
dataGridView1.DataSource = DataTable;
MyCommand.Parameters.Clear();
MyConnection.Close();
The second command parameters declaration method:
long PhoneNumber = 07429114523;
MyConnection.Open();
MyCommand.CommandText = "SELECT * FROM List_of_All_Orders WHERE Phone= @PhoneNumber ";
MyCommand.Connection = MyConnection;
// command parameters declaration and value assigning
MyCommand.Parameters.Add("@PhoneNumber", SqlDbType.BigInt);
MyCommand.Parameters["@PhoneNumber"].Value = PhoneNumber;
Sql_Data_Adapter.SelectCommand = MyCommand;
Sql_Data_Adapter.Fill(DataTable);
dataGridView1.DataSource = DataTable;
MyCommand.Parameters.Clear();
MyConnection.Close();
Anyone knows which method to choose first or second in terms of command parameters declaration and value assigning and which one is safer.
Please help. Thank you