Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I've a problem with my project in c#, specifically with the creation of a database and the interaction with it. I would like some tips and guidelines.
Starting with the kind of db:
-)premise that the bulk of my data is quite small, wich kind of database should I create? (I prefer work with no local database, and sql server)

Let's connection with the project: how should i connect my project with the database? Could someone give me some guidelines, and some code's example?


Thanks in advance to all

Alessio
Posted

1 solution

If you have SQL Server, then use an SQL server database. Unless you are going to be distributing your application, and each instance will use the DB as a standalone store, in which case SQL server is a bit overkill. Then I would use SQLCE or SQLite instead.
For SQL server databases:
C#
using (SqlConnection con = new SqlConnection(strCon))
    {
    con.Open();
    string strcmd = "SELECT fileName FROM myTable WHERE iD=@ID";
    using (SqlCommand cmd = new SqlCommand(strcmd, con))
        {
        cmd.Parameters.AddWithValue("@ID", iD);
        using (SqlDataReader r = cmd.ExecuteReader())
            {
            while (r.Read())
                {
                Console.Write((string) r["fileName"]);
                }
            }
        }
    }
 
Share this answer
 
Comments
alessio.gastaldo 25-Aug-11 7:19am    
Thanks for the help! I've another answer, sorry.
How have you composed the string strCon? Could you give me an example?

Thanks in advance

Alessio
OriginalGriff 25-Aug-11 7:27am    
strCon is just the straight connection string needed to access the database - I read it directly from web.config or by application configuration as appropriate. Mine would be no use to you at all! :laugh:
If you need to find out what your string is:
1) In Visual Studio, open the Server Explorer pane
2) Connect to your database, and highlight it in the Server Explorer pane.
3) The properties pane will have your connection string, and you can copy and paste it from there.
alessio.gastaldo 25-Aug-11 7:41am    
Thanks I'll try to follow your tips!

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