C# Advance Functions





Def:

public void fun_name(string s)
        {
        }

Calling:
fun_name("abcd");


public string fun_name(string s,string s2="kart")
        {
            return s = s +s2;
        }

Calling:
fun_name("abcd");
fun_name("abcd",”12345”);

public  string fun_name(params object[] values)
        {
            return values[0].ToString();
        }

Calling:
fun_name("abcd");
fun_name(50);
fun_name(50.5568);
fun_name("abcd",50,50.2145);

public static class ObjectExtensions
    {
        /// <summary>
        /// convert object to string with null safe and it return string
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
    
        public static string ToSafeString(this object obj)
        {
            return obj != null ? obj.ToString() : String.Empty;
        }
        /// <summary>
        /// convert object to string with null safe, if can't return 0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToSafeString_0(this object obj)
        {
            return obj != null ? obj.ToString() : "0";
        }
    }


Calling:
.ToSafeString();


int a=null;
a.ToSafeString();
a.ToSafeString_0();

windows form application C# with SQL Database

windows form application C# with SQL Database




SQLCE
Queries protection from injection
Group of Queries execute in single Transaction
---------------------------------------------------------------------


private void button1_Click(object sender, EventArgs e)
        {

            SqlCeCommand[] cmd = new SqlCeCommand[500];
            for (int i = 0; i <3; i++)
            {               
                cmd[i] = new SqlCeCommand();               
                cmd[i].CommandText = "insert into kart(name, city)values(@name,@city)";
                cmd[i].Parameters.AddWithValue("@name", name_txt.Text);
                cmd[i].Parameters.AddWithValue("@city", city_txt.Text);              
            }
            arrycmd(cmd);
        }


        void arrycmd(SqlCeCommand[] cmd_array)
        {
            //connection check and open
            if (con.State == ConnectionState.Open) { con.Close(); } con.Open();
                       
            //create transaction
            SqlCeTransaction transaction;
            transaction = con.BeginTransaction(IsolationLevel.ReadCommitted);
          
            int m = cmd_array.Length;
            try
            {
                for (int i = 0; i < m; i++)
                {
                    if (cmd_array[i] == null)
                        break;
                    // Assign connection  and transaction to the current command
                    cmd_array[i].Connection = con;
                    cmd_array[i].Transaction = transaction;
                    cmd_array[i].ExecuteNonQuery();

                    cmd_array[i].Dispose();
                }

                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
            }

            con.Close();
            MessageBox.Show("done");
        }
      

    }



Your IP Address is:

Browser: