Кажется, что некоторые функции отсутствуют, например, вы никогда не создаете объект Уведомления.Если вы используете неявно созданные переменные в VB, это, скорее всего, не сработает для преобразования в C # (и в VB также не рекомендуется).Кроме того, я бы посоветовал вам использовать Using
блоки, чтобы убедиться, что все закрыто и расположено правильно:
<WebMethod()> _
Public Function GetNotifications() As List(Of Notification)
Dim notifications As New List(Of Notification)()
Using connection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))
Using command = New SqlCommand("select * from table1 where col1 = @user", connection)
command.Parameters.Add("@user", SqlDbType.VarChar, 5).Value = sUser;
connection.Open()
Using reader = command.ExecuteReader()
Dim idIndex As Integer = reader.GetOrdinal("id")
Dim textIndex As Integer = reader.GetOrdinal("text")
While reader.Read()
notifications.Add(New Notification(reader.GetInt32(idIndex), reader.GetString(textIndex)))
End While
End Using
End Using
End Using
Return notifications
End Function
Теперь оно должно (в идеале) преобразоваться в:
[WebMethod()]
public List<Notification> GetNotifications() {
var notifications = new List<Notification>();
using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"])) {
using (SqlCommand command = new SqlCommand("select * from table1 where col1 = @user", connection)) {
command.Parameters.Add("@user", SqlDbType.VarChar, 5).Value = sUser;
connection.Open();
using (SqlDataReader reader = command.ExecuteReader()) {
int idIndex = reader.GetOrdinal("id")
int textIndex = reader.GetOrdinal("text")
while (reader.Read()) {
notifications.Add(new Notification(reader.GetInt32(idIndex), reader.GetString(textIndex)));
}
}
}
}
return notifications;
}
Редактировать:
Изменения сделаны:
- Изменен в скобках при доступе
AppSettings
- Изменен в скобках при доступе
objSQLDataReader
- Изменен читателькод для использования
GetOrdinal
, GetInt32
и GetString
- Измененные имена переменных;удалено венгерское обозначение