Wednesday, 13 August 2014

C#.NET Save and Retrieve Image From Database. ( Part 2 - SQL Server Database Connection)



First we have to create a database in sql server.  Or use your existing database. For my project my database name is CodeBind. Now create a table to save image named SavedImages. Table creation query is given below.
CREATE TABLE [dbo].[SavedImages] (
    [ImageName]    NVARCHAR (200) NOT NULL,
    [ImageData]    IMAGE          NULL,
    [OriginalPath] VARCHAR (200)  NULL,
    CONSTRAINT [PK_SavedImages] PRIMARY KEY CLUSTERED ([ImageName] ASC)
);


I have tried to keep it simple. So I just added only 3 columns. ImageName is my primary key for this table.
Now we have to connect with database. You can connect with your database with two ways. 1. Using ConnectionString 2.Using AppSettings
The main difference is in appsettings section we can store any data string values including database connection strings also but in connectionStrings section only database connection strings can store those are our application connection strings and new features (Membership, Personalization and Role Manager) connection strings only.

Instead of this there is no much difference between appsettings and connectionStrings.

Here is my App.config file’s data :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <add name="CodeBind.Properties.Settings.CodeBindConnectionString"
      connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CodeBind.mdf;Integrated Security=True;Connect Timeout=30"
      providerName="System.Data.SqlClient" />
   
  </connectionStrings>
  <appSettings>
    <add key="ConnString" value="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CodeBind.mdf;Integrated Security=True;Connect Timeout=30" />
  </appSettings>
</configuration>

I am using local database. Use your own connectionString. 

[ Note: You know that to use SqlConnection you must add System.Data.SqlClient namespace. using System.Data.SqlClient; ]

1 Connection to the database using ConnectionString :

ConnectionStringSettings ConnString = ConfigurationManager.ConnectionStrings["CodeBind.Properties.Settings.CodeBindConnectionString"];
SqlConnection Conn = new SqlConnection(connString);

C Connection to database using AppSettings :

string connString = null; 
connString =ConfigurationManager.AppSettings["ConnString"].ToString();
SqlConnection Conn = new SqlConnection(connString);

You may get an error on “ ConfigurationManager.AppSettings “ .To get rid of the error you have to add a reference.
Solution Explorer >> References >> (Right Click) Add reference >> (Framework/.NET tab) System.Configuration

We will create a function that will return ConnectionString.
// Using ConnectionString
public static  string GenerateString()
        {
            string returnValue = null;
            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["CodeBind.Properties.Settings.CodeBindConnectionString"];

            //If found, return the connection string.
            if (settings != null)
                returnValue = settings.ConnectionString;

            return returnValue;          
        }

// Using AppSettings
public static  string GenerateString()
        {
            string returnValue = null;
            ConnectionStringSettings settings = ConfigurationManager.AppSettings["ConnString"].ToString();

            //If found, return the connection string.
            if (settings != null)
                returnValue = settings.ConnectionString;

            return returnValue;
        }

Note: If you want to use both type of function then make sure that function names are different.

No comments:

Post a Comment