如何从c#中的List 获取bool值?
hi i am converting a vb project into c# from a youtube tutorial by mkaatr he use a variable
嗨,我正在通过mkaatr从youtube教程将vb项目转换为c#,他使用了一个变量
Private DBMSResultSets As List(Of Object)
so i use a variable in c# private List<object> DBMSResultSets;
所以我在c#private List
and later in code he use a function with return type bool and he use a method
后来在代码中他使用了返回类型为bool的函数,并使用了一个方法
return DBMSResultSets(I).Read
so i use the same thing but visual studio give me error so hover on vb code (DBMSResultSets(I).Read)
it say "get or set the element at the specified index"
所以我使用相同的东西但视觉工作室给我错误所以悬停在vb代码(DBMSResultSets(I).Read)它说“获取或设置指定索引处的元素”
so i look around and find out that if i write return DBMSResultSets[(int)I]);
it do the same thing which is ("get or set the element at the specified index")
所以我环顾四周,发现如果我写回DBMSResultSets [(int)I]);它做同样的事情(“获取或设置指定索引处的元素”)
now visual studio give me error that can not convert object to bool so i use convert.to and also try the (bool) mean try to typecast but both method didn't work so i need help i am giving you the whole vb code and also my conversion c# code
现在visual studio给我错误,无法将对象转换为bool所以我使用convert.to并尝试(bool)意味着尝试类型转换但两种方法都不起作用所以我需要帮助我给你整个vb代码和还有我的转换c#代码
the problem is in function ReadAndNotEOF
问题在于函数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
我的c#代码
//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");
}
}
}
1 个解决方案
#1
The result of calling executeReader on an SqlCommand object is a SqlDataReader
在SqlCommand对象上调用executeReader的结果是SqlDataReader
See here
In this part of your code
在这部分代码中
// 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);
The type of RS is not the exact equivalent of a ResultSet
like the accrostic suggests but it is a SqlDataReader
which has a boolean method call Read
and it will return true until there is no more datas to fetch from its stream
RS的类型并不是结果集的完全等价,就像应用程序所暗示的那样,但它是一个SqlDataReader,它有一个布尔方法调用Readand它将返回true,直到没有更多的数据从其流中获取
So here in your boolean method called ReadAndNotEof
instead of
所以在这里你的布尔方法叫做ReadAndNotEofinstead of
return Convert.ToBoolean(DBMSResultSets[(int)I]);
do
SqlDataReader reader = (SqlDataReader)DBMSResultSets[(int)I];
return reader.Read();
On the same pattern you can get the column value in your method GetColumnValue if your change:
在相同的模式下,如果您的更改,您可以在方法GetColumnValue中获取列值:
return DBMSResultSets[(int)I].Equals(ColumnName);
by
SqlDataReader reader = (SqlDataReader)DBMSResultSets[(int)I];
object columnValue = reader[columnName];
return columnValue;
See here on msdn for the definition of the accessor this
(brackets like reader[string_value]) that takes a string parameter (the column name)
请参阅msdn,了解访问器的定义(括号如reader [string_value]),它带有一个字符串参数(列名)
Of course you need to be sure that the Read method of your SqlDataReader has returned true (your above method)
当然你需要确保你的SqlDataReader的Read方法返回true(你的上面的方法)
Hope this helps you.
希望这对你有所帮助。
更多相关文章
- mysql随机取出若干条记录的实用方法
- mysql获取自增id最大值四种方法
- 一条牛B的SQL抵了我300多行的程序代码
- Android中RecyclerView的item中控件的点击事件添加删除一行、上
- Android Studio 出现 Gradle's dependency cache may be corrupt
- Android Service使用方法--简单音乐播放实例
- 绑定本地Service和远程Service并调用其中方法
- Android Studio中如何编写JNI代码及编译so库
- "AndroidStudio 单元测试"-最简单最快的方法-ApplicationTest