简体   繁体   中英

Connecting T-SQL to C++ or C#

Which language is preferable if Im wanting to access a T-SQL like MS Server 2008? within my code?

I know both languages probably have extensions, I do use Visual Studio 2010. However I want to know which language is probably more.....lets say "well formed" to access the database with.

Like I know C# is kinda Microsofts "thing", but I wasn't for sure. Obviously using an API that is kinda default for T-SQL is preferable, but I dont mind using Open Source.

Im more fluent in C++, but the code is relatively simple anyways.

Well, "the one you can do effectively now" would be a good start, which for you sounds like C++; I'm pretty sure both have extensive support for talking to SQL Server.

Personally I'd use C#, but that is simply because I know the libraries and tools pretty well, and could whack out a SQL call in a few seconds. I might even go so far as to suggest that the .NET libraries have seen more love lately - so maybe managed C++/CLI might help. Maybe.

It also depends on whether you can assume the client has .NET, of course...

Regardless, a simple db call should be fairly trivial - it is all those nasty corner cases that is the problem; for any platform/language, they can take experience to predict properly.

For example, in C# with a reference to System.Data.dll :

using System.Data;
using System.Data.SqlClient;
...
void SomeMethod() {
    using(var conn = new SqlConnection(connectionString))
    using(var cmd = conn.CreateCommand()) {
        cmd.CommandText = @"
           // some
           // tsql
           // here";
        // cmd.Paramaters.Add(...); // bobby tables
        conn.Open();
        cmd.ExecuteNonQuery(); // or Reader, etc - lots of options
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM