Sunday 4 January 2015

C# EASIEST WAY TO CONNECT TO A LOCAL DATABASE

i spent close to one full year doing this till i suceded and thought of making a simple tutorial that can help alot of my brothers out there
Follow These steps and you will insert to a database



1  First all right click on your project
as shown below





then add new item



step 3 click add new item and select Service based database i called mine blogger.mdf


step 4 click add and wait for the system to create after creation
double click on the database

this will open the database in the server explorer located on the top left corner of visual studio 2013
as shown below
then right click on the  as shown below and choose  modify


step 5 you will see this

change the data source to Microsoft SQL Server Database File (SqlClient)
and locate your database
after this



This is where we all get it wrong the data source is not always .\SQLEXPRESS as most tutorials give

click advanced and you will be greeted with all the information you are going to need to connect to the database

 con.ConnectionString = @"Data Source=(LocalDB)\v11.0;
                          AttachDbFilename=C:\Users\icytrey\documents\visual studio 2013\Projects\PdfToWordDocument\PdfToWordDocument\MysimpleDatabase.mdf;
                          Integrated Security=True;
                          Connect Timeout=30;";

note  AttachDbFilename locate your database.mdf file in the path



then you are almost done


here is the complete code


            System.Data.SqlClient.SqlConnection con;
            con = new System.Data.SqlClient.SqlConnection();
           
            con.ConnectionString = @"Data Source=(LocalDB)\v11.0;
                          AttachDbFilename=C:\Users\icytrey\documents\visual studio 2013\Projects\PdfToWordDocument\PdfToWordDocument\MysimpleDatabase.mdf;
                          Integrated Security=True;
                          Connect Timeout=30;";

            con.Open();
            SqlCommand addSite = new SqlCommand(@"INSERT INTO Movies (movie,category) VALUES (@name,@cat)", con);
            addSite.Parameters.AddWithValue("@name", "X men ");
            addSite.Parameters.AddWithValue("@cat", "Action");
            addSite.ExecuteNonQuery();

            //Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\icytrey\Documents\Visual Studio 2013\Projects\PdfToWordDocument\PdfToWordDocument\MysimpleDatabase.mdf";Integrated Security=True;Connect Timeout=30
           
            MessageBox.Show("Data Inserted");
            con.Close();


create the table by expanding the database right click on Tables as shown below

last step you will be greeted with this interface create the table and hit update icon

and you are done


dont forget to make id IDENTITY PRIMARY KEY

thanks hope this helps you alot


8 comments:

  1. This comment has been removed by the author.

    ReplyDelete



  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.

    angularjs-Training in velachery

    angularjs-Training in annanagar

    angularjs Training in chennai

    angularjs Training in chennai

    ReplyDelete
  3. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.
    Python training in marathahalli | Python training institute in pune

    ReplyDelete
  4. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    blueprism training in chennai | blueprism training in bangalore | blueprism training in pune | blueprism online training

    ReplyDelete
  5. Hello I am so delighted I found your blog, I really found you by mistake, while I was looking on Yahoo for something else, anyways I am here now and would just like to say thanks for a tremendous post. Please do keep up the great work.
    Java training in Indira nagar | Java training in Rajaji nagar

    Java training in Marathahalli | Java training in Btm layout

    ReplyDelete
  6. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.is article.
    Data Science training in kalyan nagar | Data Science training in OMR

    Data Science training in chennai | Data science training in velachery

    Data science training in tambaram | Data science training in jaya nagar

    ReplyDelete
  7. I am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  8. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.happy to read
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete