[Request] How to connect a C# application to Sql Server

01/12/2012 16:20 shitboi#1
Ok, the title is very vague, I'll provide the details here.

I have a MVC3 application that requires connection to a database. I have already created the necessary tables in a database in Sql Server 2008, and i have verified that MSVS2010 can detect my DB. My question is how to make my application query the DB?


When i worked on another solution, there is a sub project in that solution that handles all database connections. This project seems to be able to populate classes based on tables in the DB. Can someone enlighten me how is that done?

Though this is not directly associated with the topic CO programming. I do think that the knowledge is common across all types of programming (especially CO ps development).

EDIT: if possible, can one please provide me screenshots/video of how to properly connect my application to a sqlserver DB and query it. I don't mind what is the methodology used; be it direct query using sql commands or using C#.Net LINQ features.
01/12/2012 17:46 jackpotsvr#2
Quote:
Originally Posted by shitboi View Post
Ok, the title is very vague, I'll provide the details here.

I have a MVC3 application that requires connection to a database. I have already created the necessary tables in a database in Sql Server 2008, and i have verified that MSVS2010 can detect my DB. My question is how to make my application query the DB?


When i worked on another solution, there is a sub project in that solution that handles all database connections. This project seems to be able to populate classes based on tables in the DB. Can someone enlighten me how is that done?

Though this is not directly associated with the topic CO programming. I do think that the knowledge is common across all types of programming (especially CO ps development).

EDIT: if possible, can one please provide me screenshots/video of how to properly connect my application to a sqlserver DB and query it. I don't mind what is the methodology used; be it direct query using sql commands or using C#.Net LINQ features.
What kind of SQL server do you use? I experienced small differences between seperate SQL servers. Like mysql has some small differences to microsft sql. Though those diffrences aren't that hard to find out.

The connecting is not hard as it may sound, there are lots of tutorials based on that on youtube(look [Only registered and activated users can see links. Click Here To Register...]), but if you're more experienced in programming you can also find the neccesary script parts in SQL-based conquer online private server sources and copy the parts you need. ;]

Ofcourse you either can write it all by yourself, but I guess you won't be asking around here if you'd be capable to do so. :P

If you want to use a tutorial on youtube, make sure you use the one for your SQL.

Hope this makes it a little more clear ;)
01/13/2012 05:44 Arco.#3
Why don't you take a looka t nHibernate?
It's what I use for my server->database handling.
[Only registered and activated users can see links. Click Here To Register...]
01/13/2012 06:20 pro4never#4
Quote:
Originally Posted by Arco. View Post
Why don't you take a looka t nHibernate?
It's what I use for my server->database handling.
[Only registered and activated users can see links. Click Here To Register...]
Doubt he wants to get that advanced right now as he's just trying to figure out basic database functionality in C#...

Nhibrinate is lots of fun (albetros has lovely examples of it in use) but it's far from simple. Lots of stuff to make sure is configured correctly and then still gotta ensure that all of your tables are mapped properly to get them to work.

It's fantastic but far from an entry level solution.
01/13/2012 09:14 I don't have a username#5
Really simple example.
Code:
using System.Data.SqlClient;

public class MssqlWrapper
{
    private string connectionString;
    public MssqlWrapper(string connectionString)
    {
        this.connectionString = connectionString;
    }

    private SqlConnection CreateConnection()
    {
        return new SqlConnection(connectionString);
    }
    public MssqlWrapper Insert(string Table, string Column, object Value, out bool Success)
    {
        try
        {
            SqlConnection con = CreateConnection();
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO " + Table + " (" + Column + ") " +
                                     "Values (" + Value + ")", con);
            cmd.ExecuteNonQuery();
            Success = true;
        }
        catch
        {
            Success = false;
        }
        return this;
    }
}
01/19/2012 22:35 konsowa#6
What "I don't have a username" has posted will work if ur connecting to a MSSql server but if u need to connect to a mysql server u need to get the mysql connecter(Search google) and add the mysql.data.dll as a reference in ur project. Best thing here is that the same code applies for both ways of connecting.
01/19/2012 23:24 gabrola#7
Quote:
Originally Posted by konsowa View Post
What "I don't have a username" has posted will work if ur connecting to a MSSql server but if u need to connect to a mysql server u need to get the mysql connecter(Search google) and add the mysql.data.dll as a reference in ur project. Best thing here is that the same code applies for both ways of connecting.
SQL Server = MSSQL
He's not asking about MySQL.
01/24/2012 15:46 ineedthe#8
yyy Thank's :D