While playing in data tables in .net, it may require to have some temporary values such as records counts, primary key field name, …etc. In those situations, instead of declaring new variables in the program, it is ideal way to use “ExtendedProperties” option in datatable. The property is holding the collection (hashtable) values. Here is the syntax; DataTable.ExtendedProperties.Add (object key, object value); Examples; DataTable dtObj = new DataTable("employees"); dtObj.ExtendedProperties.Add("totalCount", 500); dtObj.ExtendedProperties.Add("primaryKey", "employee_id"); dtObj.ExtendedProperties.Add("createdDate", DateTime.Now); For retrieving assigned properties, int iCount = Convert.ToInt32(dtObj.ExtendedProperties["totalCount"].ToString()); So simple and effective property.
We may need to store the images in the database. It is easy and pretty simple. The basic idea is; 1) The images are stored in database as bytes. In MySQL database we need to create a field/column of type " Blob ". 2) Need to use FileStream and BinaryReader objects to convert images into bytes. Here is the C# code, to do the task Table schema: C# code: MySqlConnection mcon = null; MySqlCommand cmd = null; FileStream fsObj = null; BinaryReader binRdr = null; try { //converting image to bytes fsObj = File.OpenRead(pictureBox1.ImageLocation); byte[] imgContent = new byte[fsObj.Length]; binRdr = new BinaryReader(fsObj); imgContent = binRdr.ReadBytes((int)fsObj.Length); mcon = new MySqlConnection("server=localhost;user=root;pwd=root;database=test;"); mcon.Open(); //inserting into MySQL db cmd = new MySqlCommand("insert into users (userid,username,userphoto) values (@userid, @username, @userphoto)", mcon); cmd.Parameters.Add(new MySqlPara...
When we are doing the coding, we need to use our own functions/methods to do a certain task. But while writing the function we may missed out the function details like why the function has been written . So whenever writing a function, if we use the documentation comments standard, it will help in the future whoever uses the function. Here is syntax; /// /// documentation block /// Here is the simple function defined in C# IDE; documentation comments added here; Intellisense is displaying the documentation comments.
Comments
Post a Comment