如何从 C# 中的 List<object> 获取 bool 值?
how to get bool value from List<object> in c#?
您好,我正在将一个 vb 项目从 mkaatr 的 youtube 教程转换为 c#
他使用变量
Private DBMSResultSets As List(Of Object)
所以我在 C# 中使用了一个变量 private List<object> DBMSResultSets;
后来在代码中他使用了一个 return 类型 bool 的函数并且他使用了一个方法
return DBMSResultSets(I).Read
所以我使用相同的东西但是 visual studio 给我错误所以悬停在 vb 代码 (DBMSResultSets(I).Read)
它说“获取或设置指定索引处的元素”
所以我环顾四周,发现如果我写 return DBMSResultSets[(int)I]);
它会做同样的事情,即(“获取或设置指定索引处的元素”)
现在 visual studio 给我一个无法将对象转换为 bool 的错误,所以我使用 convert.to 并尝试使用 (bool) 表示尝试进行类型转换,但两种方法均无效
所以我需要帮助
我给你整个 vb 代码和我的转换 c# 代码
the problem is in function ReadAndNotEOF
vb code
Imports System.Data.SqlClient
' this class will be used to manage connectivity with the database
Public Class DBMSClass
' define the connection string
Private DBMSConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Asus\Desktop\LibraryManagementSystem\Database\LMS.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=true"
' define the connection
Private DBMSConnectionObj As System.Data.SqlClient.SqlConnection
' define the transaction
Private DBMSTransactionObj As System.Data.SqlClient.SqlTransaction
' define the commands object and result sets
Private DBMSCommands As List(Of System.Data.SqlClient.SqlCommand)
Private DBMSCommandCodes As List(Of Long)
Private DBMSResultSets As List(Of Object)
' command counter
Private DBMSCommandCounter As Long
' open database connection
Public Function OpenDB() As String
Try
' open the connection
DBMSConnectionObj = New SqlConnection(My.Settings.myconnection)
DBMSConnectionObj.Open()
' create the transaction
DBMSTransactionObj = DBMSConnectionObj.BeginTransaction
' prepare the commands list
DBMSCommands = New List(Of System.Data.SqlClient.SqlCommand)
DBMSCommandCodes = New List(Of Long)
DBMSResultSets = New List(Of Object)
' prepare the command counter
DBMSCommandCounter = 0
' return ok
Return "OK"
Catch ex As Exception
Return ex.Message
End Try
End Function
' this is used to run sql commands
Public Sub ExecuteSQL(ByVal SQL As String, ByVal ParamArray Obj() As Object)
' build the command object
Dim CMD As New System.Data.SqlClient.SqlCommand(SQL, Me.DBMSConnectionObj, Me.DBMSTransactionObj)
' add the parameters to the sql command
Dim I As Integer
For I = 0 To Obj.Length - 1
CMD.Parameters.AddWithValue("@" & I, Obj(I))
Next
' run the sql
CMD.ExecuteNonQuery()
End Sub
' this function is used to commit a transaction
Public Sub Commit()
Me.DBMSTransactionObj.Commit()
Me.DBMSTransactionObj = Me.DBMSConnectionObj.BeginTransaction
End Sub
' this function is used to rollback a transaction
Public Sub Rollback()
Me.DBMSTransactionObj.Rollback()
Me.DBMSTransactionObj = Me.DBMSConnectionObj.BeginTransaction
End Sub
' this function is used to create a result set
Public Function CreateResultSet(ByVal SQL As String, ByVal ParamArray OBJ() As Object) As Long
DBMSCommandCounter += 1
' build the command object
Dim CMD As New System.Data.SqlClient.SqlCommand(SQL, Me.DBMSConnectionObj, Me.DBMSTransactionObj)
' add the parameters to the sql command
Dim I As Integer
For I = 0 To OBJ.Length - 1
CMD.Parameters.AddWithValue("@" & I, OBJ(I))
Next
' read the data
Dim RS = CMD.ExecuteReader(CommandBehavior.Default)
' store objects in list
Me.DBMSCommandCodes.Add(DBMSCommandCounter)
Me.DBMSCommands.Add(CMD)
Me.DBMSResultSets.Add(RS)
Return DBMSCommandCounter
End Function
' this function is used to close a result set
Public Sub CloseResultSet(ByVal Nmbr As Long)
Dim I As Integer
For I = 0 To Me.DBMSCommandCodes.Count - 1
' find the command and result set
If DBMSCommandCodes(I) = Nmbr Then
' get the objects
Dim R = Me.DBMSResultSets(I)
Dim C = Me.DBMSCommands(I)
' remove the objects from the list
Me.DBMSResultSets.RemoveAt(I)
Me.DBMSCommands.RemoveAt(I)
Me.DBMSCommandCodes.RemoveAt(I)
' return the resources
R.Close()
R.Dispose()
C.Dispose()
Return
End If
Next
Throw New Exception("the command or result set does not exist")
End Sub
' this function is used to read a single record from db
Public Function ReadAndNotEOF(ByVal Code As Long) As Boolean
' do a search
Dim I As Long
For I = 0 To Me.DBMSCommandCodes.Count - 1
If DBMSCommandCodes(I) = Code Then
Return DBMSResultSets(I).Read
End If
Next
Throw New Exception("Command or Resultset does not exist")
End Function
' this function is used to get a column value from db
Public Function GetColumnValue(ByVal Code As Long, ByVal ColumnName As String) As Object
Dim I As Long
For I = 0 To Me.DBMSCommands.Count - 1
If DBMSCommandCodes(I) = Code Then
Return DBMSResultSets(I).Item(ColumnName)
End If
Next
Throw New Exception("Command or Resultset does not exist")
End Function
End Class
my c# code
//this class will be used to manage connectivity with the database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
namespace Library_main
{
public class DBMSClass
{
//define the connection string
// private String DBMSConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename="\D:\tutorial\c # tutorial\3 may 2015\Library_main\Library_main\bin\Debug\DataBase\LMS.mdf";"Integrated Security=True;Connect Timeout=30";
//define the connection
private SqlConnection DBMSConnectionObj = null;
//define the transaction
private SqlTransaction DBMSTransactionObj;
// define the commands object and result sets
private List<SqlCommand> DBMSCommands;
private List<long> DBMSCommandCodes;
private List<object> DBMSResultSets;
// command counter
private long DBMSCommandCounter;
//open database connection
public string OpenDB()
{
try
{
//open the connection
DBMSConnectionObj = new SqlConnection(Properties.Settings.Default.ConnectionString);
DBMSConnectionObj.Open();
//creat the transaction
DBMSTransactionObj = DBMSConnectionObj.BeginTransaction();
//prepare the commands list
DBMSCommands = new List<SqlCommand>();
DBMSCommandCodes = new List<long>();
DBMSResultSets = new List<object>();
// prepare the command counter
DBMSCommandCounter = 0;
//return ok
return "ok";
}
catch (Exception ex)
{
return ex.Message;
}
}
//this is used to run sql commands
public void ExceuteSQL(string SQL, params object[] Obj)
{
//build the command object
SqlCommand CMD = new SqlCommand(SQL, this.DBMSConnectionObj, this.DBMSTransactionObj);
//add the parameters to the sql command
int I;
int count = Obj.Length - 1;
for (I = 0; I <= count; I++)
{
CMD.Parameters.AddWithValue("@" + I, Obj[I]);
}
//run the sql
CMD.ExecuteNonQuery();
}
//this funtion to commit
public void Commit()
{
this.DBMSTransactionObj.Commit();
this.DBMSTransactionObj = this.DBMSConnectionObj.BeginTransaction();
}
// this function is used to rollback a transaction
public void Rollback()
{
this.DBMSTransactionObj.Rollback();
this.DBMSTransactionObj = this.DBMSConnectionObj.BeginTransaction();
}
//this function is used to creat a result set
public long CreatResultSet(string SQL, params object[] Obj)
{
DBMSCommandCounter += 1;
// build the command object
SqlCommand CMD = new SqlCommand(SQL, this.DBMSConnectionObj, this.DBMSTransactionObj);
// add the parameters to the sql command
int I = 0;
for (I = 0; I <= Obj.Length - 1; I++)
{
CMD.Parameters.AddWithValue("@" + I, Obj[I]);
}
// read the data
dynamic RS = CMD.ExecuteReader(System.Data.CommandBehavior.Default);
// store objects in list
this.DBMSCommandCodes.Add(DBMSCommandCounter);
this.DBMSCommands.Add(CMD);
this.DBMSResultSets.Add(RS);
return DBMSCommandCounter;
}
// this function is used to close a result set
public void CloseResultSet(long Nmbr)
{
int I = 0;
for (I = 0; I <= this.DBMSCommandCodes.Count - 1; I++)
{
// find the command and result set
if (DBMSCommandCodes[I] == Nmbr)
{
// get the objects
dynamic R = this.DBMSResultSets[I];
dynamic C = this.DBMSCommands[I];
// remove the objects from the list
this.DBMSResultSets.RemoveAt(I);
this.DBMSCommands.RemoveAt(I);
this.DBMSCommandCodes.RemoveAt(I);
// return the resources
R.Close();
R.Dispose();
C.Dispose();
return;
}
}
throw new Exception("the command or result set does not exist");
}
// this function is used to read a single record from db
public bool ReadAndNotEOF(long Code)
{
// do a search
long I = 0;
for (I = 0; I <= this.DBMSCommandCodes.Count - 1; I++)
{
if (DBMSCommandCodes[(int)I] == Code)
{
return Convert.ToBoolean(DBMSResultSets[(int)I]);
}
}
throw new Exception("Command or Resultset does not exist");
}
// this function is used to get a column value from db
public object GetColumnValue(long Code, string ColumnName)
{
long I = 0;
for (I = 0; I <= this.DBMSCommandCodes.Count - 1; I++)
if (DBMSCommandCodes[(int)I] == Code)
{
return DBMSResultSets[(int)I].Equals(ColumnName);
}
throw new Exception("Command or Resultset does not exist");
}
}
}
在 SqlCommand 对象上调用 executeReader 的结果是 SqlDataReader
见here
在你的这部分代码中
// read the data
dynamic RS = CMD.ExecuteReader(System.Data.CommandBehavior.Default);
// store objects in list
this.DBMSCommandCodes.Add(DBMSCommandCounter);
this.DBMSCommands.Add(CMD);
this.DBMSResultSets.Add(RS);
RS 的类型并不完全等同于 ResultSet
,就像 accrostic 所暗示的那样,但它是一个 SqlDataReader
,它有一个布尔方法调用 Read
,它将 return true 直到没有更多的数据可以从它的流中获取
所以在你的布尔方法中调用 ReadAndNotEof
而不是
return Convert.ToBoolean(DBMSResultSets[(int)I]);
做
SqlDataReader reader = (SqlDataReader)DBMSResultSets[(int)I];
return reader.Read();
在相同的模式下,您可以在方法 GetColumnValue 中获取列值,如果您更改:
return DBMSResultSets[(int)I].Equals(ColumnName);
来自
SqlDataReader reader = (SqlDataReader)DBMSResultSets[(int)I];
object columnValue = reader[columnName];
return columnValue;
请参阅 here on msdn 以获取带有字符串参数(列名)
当然,您需要确保 SqlDataReader 的 Read 方法return为真(您的上述方法)
希望对您有所帮助。
您好,我正在将一个 vb 项目从 mkaatr 的 youtube 教程转换为 c# 他使用变量
Private DBMSResultSets As List(Of Object)
所以我在 C# 中使用了一个变量 private List<object> DBMSResultSets;
后来在代码中他使用了一个 return 类型 bool 的函数并且他使用了一个方法
return DBMSResultSets(I).Read
所以我使用相同的东西但是 visual studio 给我错误所以悬停在 vb 代码 (DBMSResultSets(I).Read)
它说“获取或设置指定索引处的元素”
所以我环顾四周,发现如果我写 return DBMSResultSets[(int)I]);
它会做同样的事情,即(“获取或设置指定索引处的元素”)
现在 visual studio 给我一个无法将对象转换为 bool 的错误,所以我使用 convert.to 并尝试使用 (bool) 表示尝试进行类型转换,但两种方法均无效 所以我需要帮助 我给你整个 vb 代码和我的转换 c# 代码
the problem is in function ReadAndNotEOF
vb code
Imports System.Data.SqlClient
' this class will be used to manage connectivity with the database
Public Class DBMSClass
' define the connection string
Private DBMSConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Asus\Desktop\LibraryManagementSystem\Database\LMS.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=true"
' define the connection
Private DBMSConnectionObj As System.Data.SqlClient.SqlConnection
' define the transaction
Private DBMSTransactionObj As System.Data.SqlClient.SqlTransaction
' define the commands object and result sets
Private DBMSCommands As List(Of System.Data.SqlClient.SqlCommand)
Private DBMSCommandCodes As List(Of Long)
Private DBMSResultSets As List(Of Object)
' command counter
Private DBMSCommandCounter As Long
' open database connection
Public Function OpenDB() As String
Try
' open the connection
DBMSConnectionObj = New SqlConnection(My.Settings.myconnection)
DBMSConnectionObj.Open()
' create the transaction
DBMSTransactionObj = DBMSConnectionObj.BeginTransaction
' prepare the commands list
DBMSCommands = New List(Of System.Data.SqlClient.SqlCommand)
DBMSCommandCodes = New List(Of Long)
DBMSResultSets = New List(Of Object)
' prepare the command counter
DBMSCommandCounter = 0
' return ok
Return "OK"
Catch ex As Exception
Return ex.Message
End Try
End Function
' this is used to run sql commands
Public Sub ExecuteSQL(ByVal SQL As String, ByVal ParamArray Obj() As Object)
' build the command object
Dim CMD As New System.Data.SqlClient.SqlCommand(SQL, Me.DBMSConnectionObj, Me.DBMSTransactionObj)
' add the parameters to the sql command
Dim I As Integer
For I = 0 To Obj.Length - 1
CMD.Parameters.AddWithValue("@" & I, Obj(I))
Next
' run the sql
CMD.ExecuteNonQuery()
End Sub
' this function is used to commit a transaction
Public Sub Commit()
Me.DBMSTransactionObj.Commit()
Me.DBMSTransactionObj = Me.DBMSConnectionObj.BeginTransaction
End Sub
' this function is used to rollback a transaction
Public Sub Rollback()
Me.DBMSTransactionObj.Rollback()
Me.DBMSTransactionObj = Me.DBMSConnectionObj.BeginTransaction
End Sub
' this function is used to create a result set
Public Function CreateResultSet(ByVal SQL As String, ByVal ParamArray OBJ() As Object) As Long
DBMSCommandCounter += 1
' build the command object
Dim CMD As New System.Data.SqlClient.SqlCommand(SQL, Me.DBMSConnectionObj, Me.DBMSTransactionObj)
' add the parameters to the sql command
Dim I As Integer
For I = 0 To OBJ.Length - 1
CMD.Parameters.AddWithValue("@" & I, OBJ(I))
Next
' read the data
Dim RS = CMD.ExecuteReader(CommandBehavior.Default)
' store objects in list
Me.DBMSCommandCodes.Add(DBMSCommandCounter)
Me.DBMSCommands.Add(CMD)
Me.DBMSResultSets.Add(RS)
Return DBMSCommandCounter
End Function
' this function is used to close a result set
Public Sub CloseResultSet(ByVal Nmbr As Long)
Dim I As Integer
For I = 0 To Me.DBMSCommandCodes.Count - 1
' find the command and result set
If DBMSCommandCodes(I) = Nmbr Then
' get the objects
Dim R = Me.DBMSResultSets(I)
Dim C = Me.DBMSCommands(I)
' remove the objects from the list
Me.DBMSResultSets.RemoveAt(I)
Me.DBMSCommands.RemoveAt(I)
Me.DBMSCommandCodes.RemoveAt(I)
' return the resources
R.Close()
R.Dispose()
C.Dispose()
Return
End If
Next
Throw New Exception("the command or result set does not exist")
End Sub
' this function is used to read a single record from db
Public Function ReadAndNotEOF(ByVal Code As Long) As Boolean
' do a search
Dim I As Long
For I = 0 To Me.DBMSCommandCodes.Count - 1
If DBMSCommandCodes(I) = Code Then
Return DBMSResultSets(I).Read
End If
Next
Throw New Exception("Command or Resultset does not exist")
End Function
' this function is used to get a column value from db
Public Function GetColumnValue(ByVal Code As Long, ByVal ColumnName As String) As Object
Dim I As Long
For I = 0 To Me.DBMSCommands.Count - 1
If DBMSCommandCodes(I) = Code Then
Return DBMSResultSets(I).Item(ColumnName)
End If
Next
Throw New Exception("Command or Resultset does not exist")
End Function
End Class
my c# code
//this class will be used to manage connectivity with the database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
namespace Library_main
{
public class DBMSClass
{
//define the connection string
// private String DBMSConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename="\D:\tutorial\c # tutorial\3 may 2015\Library_main\Library_main\bin\Debug\DataBase\LMS.mdf";"Integrated Security=True;Connect Timeout=30";
//define the connection
private SqlConnection DBMSConnectionObj = null;
//define the transaction
private SqlTransaction DBMSTransactionObj;
// define the commands object and result sets
private List<SqlCommand> DBMSCommands;
private List<long> DBMSCommandCodes;
private List<object> DBMSResultSets;
// command counter
private long DBMSCommandCounter;
//open database connection
public string OpenDB()
{
try
{
//open the connection
DBMSConnectionObj = new SqlConnection(Properties.Settings.Default.ConnectionString);
DBMSConnectionObj.Open();
//creat the transaction
DBMSTransactionObj = DBMSConnectionObj.BeginTransaction();
//prepare the commands list
DBMSCommands = new List<SqlCommand>();
DBMSCommandCodes = new List<long>();
DBMSResultSets = new List<object>();
// prepare the command counter
DBMSCommandCounter = 0;
//return ok
return "ok";
}
catch (Exception ex)
{
return ex.Message;
}
}
//this is used to run sql commands
public void ExceuteSQL(string SQL, params object[] Obj)
{
//build the command object
SqlCommand CMD = new SqlCommand(SQL, this.DBMSConnectionObj, this.DBMSTransactionObj);
//add the parameters to the sql command
int I;
int count = Obj.Length - 1;
for (I = 0; I <= count; I++)
{
CMD.Parameters.AddWithValue("@" + I, Obj[I]);
}
//run the sql
CMD.ExecuteNonQuery();
}
//this funtion to commit
public void Commit()
{
this.DBMSTransactionObj.Commit();
this.DBMSTransactionObj = this.DBMSConnectionObj.BeginTransaction();
}
// this function is used to rollback a transaction
public void Rollback()
{
this.DBMSTransactionObj.Rollback();
this.DBMSTransactionObj = this.DBMSConnectionObj.BeginTransaction();
}
//this function is used to creat a result set
public long CreatResultSet(string SQL, params object[] Obj)
{
DBMSCommandCounter += 1;
// build the command object
SqlCommand CMD = new SqlCommand(SQL, this.DBMSConnectionObj, this.DBMSTransactionObj);
// add the parameters to the sql command
int I = 0;
for (I = 0; I <= Obj.Length - 1; I++)
{
CMD.Parameters.AddWithValue("@" + I, Obj[I]);
}
// read the data
dynamic RS = CMD.ExecuteReader(System.Data.CommandBehavior.Default);
// store objects in list
this.DBMSCommandCodes.Add(DBMSCommandCounter);
this.DBMSCommands.Add(CMD);
this.DBMSResultSets.Add(RS);
return DBMSCommandCounter;
}
// this function is used to close a result set
public void CloseResultSet(long Nmbr)
{
int I = 0;
for (I = 0; I <= this.DBMSCommandCodes.Count - 1; I++)
{
// find the command and result set
if (DBMSCommandCodes[I] == Nmbr)
{
// get the objects
dynamic R = this.DBMSResultSets[I];
dynamic C = this.DBMSCommands[I];
// remove the objects from the list
this.DBMSResultSets.RemoveAt(I);
this.DBMSCommands.RemoveAt(I);
this.DBMSCommandCodes.RemoveAt(I);
// return the resources
R.Close();
R.Dispose();
C.Dispose();
return;
}
}
throw new Exception("the command or result set does not exist");
}
// this function is used to read a single record from db
public bool ReadAndNotEOF(long Code)
{
// do a search
long I = 0;
for (I = 0; I <= this.DBMSCommandCodes.Count - 1; I++)
{
if (DBMSCommandCodes[(int)I] == Code)
{
return Convert.ToBoolean(DBMSResultSets[(int)I]);
}
}
throw new Exception("Command or Resultset does not exist");
}
// this function is used to get a column value from db
public object GetColumnValue(long Code, string ColumnName)
{
long I = 0;
for (I = 0; I <= this.DBMSCommandCodes.Count - 1; I++)
if (DBMSCommandCodes[(int)I] == Code)
{
return DBMSResultSets[(int)I].Equals(ColumnName);
}
throw new Exception("Command or Resultset does not exist");
}
}
}
在 SqlCommand 对象上调用 executeReader 的结果是 SqlDataReader
见here
在你的这部分代码中
// read the data
dynamic RS = CMD.ExecuteReader(System.Data.CommandBehavior.Default);
// store objects in list
this.DBMSCommandCodes.Add(DBMSCommandCounter);
this.DBMSCommands.Add(CMD);
this.DBMSResultSets.Add(RS);
RS 的类型并不完全等同于 ResultSet
,就像 accrostic 所暗示的那样,但它是一个 SqlDataReader
,它有一个布尔方法调用 Read
,它将 return true 直到没有更多的数据可以从它的流中获取
所以在你的布尔方法中调用 ReadAndNotEof
而不是
return Convert.ToBoolean(DBMSResultSets[(int)I]);
做
SqlDataReader reader = (SqlDataReader)DBMSResultSets[(int)I];
return reader.Read();
在相同的模式下,您可以在方法 GetColumnValue 中获取列值,如果您更改:
return DBMSResultSets[(int)I].Equals(ColumnName);
来自
SqlDataReader reader = (SqlDataReader)DBMSResultSets[(int)I];
object columnValue = reader[columnName];
return columnValue;
请参阅 here on msdn 以获取带有字符串参数(列名)
当然,您需要确保 SqlDataReader 的 Read 方法return为真(您的上述方法)
希望对您有所帮助。