I have this code that compiles in VB.NET:
Imports System
Imports System.Data
Imports System.Data.Entity
Imports System.Data.SqlClient
Imports System.Linq
Imports System.Collections
Imports System.Collections.Generic
Friend Module MainModule
Friend Sub Main(args As String())
Dim ds = GetSqlDataSet("", "")
Dim allRows = From row In ds.Tables(0) Select row
End Sub
Private Function GetSqlDataSet(ByVal forQuery As String,
ByVal withConnectionString As String,
ByVal ParamArray withParameters As SqlClient.SqlParameter()) As DataSet
GetSqlDataSet = New DataSet()
Using conn As New System.Data.SqlClient.SqlConnection(withConnectionString)
Using command As New System.Data.SqlClient.SqlCommand(forQuery, conn)
command.Parameters.AddRange(withParameters)
Using dataAdaptor As New System.Data.SqlClient.SqlDataAdapter(command)
dataAdaptor.Fill(GetSqlDataSet)
End Using
End Using
End Using
End Function
End Module
Here are the links:

Now I have what seems like the exact equivalent in C #:
using System;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication1
{
internal static class MainEntryPoint
{
internal static void Main(string[] args)
{
var ds = GetSqlServerDataSet("", "");
var allRows = from row in ds.Tables[0] select row;
}
public static System.Data.DataSet GetSqlServerDataSet(string usingQuery,
string usingConnectionString, params System.Data.SqlClient.SqlParameter[] withParameters)
{
var ret = new System.Data.DataSet();
using (var conn = new System.Data.SqlClient.SqlConnection(usingConnectionString))
{
using (var command = new System.Data.SqlClient.SqlCommand(usingQuery, conn))
{
command.Parameters.AddRange(withParameters);
using (var adapter = new System.Data.SqlClient.SqlDataAdapter(command))
{
adapter.Fill(ret);
}
}
}
return ret;
}
}
}
And here are the links:

But I get this error:

I found a lot of resources that talk about adding link / use operators forSystem.Linq and as wellSystem.Data.Entity , but obviously I have those in both cases. Can someone please help me shed some light on this? Why does it work in VB.NET and not C #, and how do I get it to work in C #?
source
share