开发者

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 don开发者_JS百科t 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
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜