Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everybody, I am new to the database access so please forgive me for these question and if the seem basic.
I have been following some examples how to access a database using the sql commands.
However I do not quite understand firstly how to create a database "FreezerTemps", and what information I am required to add in the SqlConnection in order to get access to it.

I create the database using the Microsoft Visual C# 2010 Express, this is located in the directory of the code itself. I believe the Microsoft SQL Server 2008 is running as well but did not change anything there, as I am very new to this concept.

I attach the code I am using below.

Thank you for reading my question and code.

Kind regards,
Stelios.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WebDatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection cs = new SqlConnection("Data Source=nemesisfc; Initial Catalog=FreezerTemps; Integrated Security=TRUE");
            cs.Open();
            MessageBox.Show(cs.State.ToString());
            cs.Close();


         }
    }
}
Posted

C#
SqlConnection cs = new SqlConnection("Data Source=servername; Initial Catalog=databasename; Integrated Security=TRUE");
            cs.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "write ur sql query here";
            cmd.ExecuteNonQuery();// for inser/delete/update
            cs.Close();
            //-------------------------------------------------

            SqlConnection cs1 = new SqlConnection("Data Source=nemesisfc; Initial Catalog=FreezerTemps; Integrated Security=TRUE");
            SqlCommand cmd1 = new SqlCommand();
            cs1.Open();
            cmd1.CommandText = "write ur sql select query here";
            DataSet dtst = new DataSet();
            SqlDataAdapter dp = new SqlDataAdapter(cmd1);
            dp.Fill(dtst);// for select query

            cs1.Close();


[edit]code wrapped in pre tags[/edit]
 
Share this answer
 
v2
Follow the steps...
1.Click on Server Explorer on right side
2.Right click on you database file *.mdf and click on properties
3.Right side of your IDE properties of *.mdf will be available Copy the Connection string and paste it on your SQL Connection() as shown

SqlConnection cs1 = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\bruda\Documents\Visual Studio 2008\WebSites\sqldemo\App_Data\Database.mdf";Integrated Security=True;User Instance=True");

This is the solution for your question Do not forget to mark it as answer If possible rate this answer

Thank you :-)
 
Share this answer
 
Comments
Member 7808930 8-Apr-11 5:55am    
Thank you Uday,
I have tried your method
As suggested I did the following

1. For my version Database Explorer
2. Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\nemesis\Desktop\WebDatabase\WebDatabase\Database1.mdf;Integrated Security=True;User Instance=True
3. SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\nemesis\\Desktop\\WebDatabase\\WebDatabase\\Database1.mdf;Integrated Security=True;User Instance=True");

And the server opens the database, I will try to write to it later this evening.
Thank you so much for you time and effort.

I will try the other methods and suggestions as well.

Thank you.
Stelios
Hi,

I understood that you have questions about SQL Server itself and how to create a database in it. For a small tutorial, have a look at this site: http://www.quackit.com/sql_server/sql_server_2008/tutorial/[^]

And to manage Sql Server this one is a 'must' download: Microsoft® SQL Server® 2008 Management Studio Express [^]

As already suggested you can use also server explorer to see the connection string to use in the application. Also one good source for connection strings is: http://www.connectionstrings.com/[^]

And for using ADO.NET, have a look at these documents: http://msdn.microsoft.com/en-us/library/kb9s9ks0.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900