类库无法正常运行 [英] Class library not functioning properly
问题描述
大家好...我试图编写一个类库,在该类库中,我处理到两个不同DB的两个不同连接.这是一些库代码...
hello guys... im trying to write a class library inwhich I handle two different connections to two different DBs. Here is some of the library code...
SqlConnection myConnection1,myConnection2;
public bool OpenAdminConnection()
{
myConnection1 = new SqlConnection("Data Source=muze-pc\\SqlExpress; Initial Catalog=adminDB; Integrated Security=SSPI");
try
{
myConnection1.Open();
SetData(); //a function used, working properly
return true;
}
catch(Exception e)
{
myConnection1.Close();
return false;
}
}
/////////////////////Second connection to 2nd DB
public void OpenTracksConnection()
{
myConnection2 = new SqlConnection("Data Source=muze-pc\\SqlExpress; Initial Catalog=MusicDB; Integrated Security=SSPI; MultipleActiveResultSets=True");
try
{
myConnection2.Open();
}
catch (Exception e)
{
myConnection2.Close();
}
}
public int GetTotalRows()//returns number of (already distinct) records
{
SqlCommand myComm = new SqlCommand("SELECT COUNT(*) FROM Tracks", myConnection2);
int totalRows = 0;
SqlDataReader dataReader;
dataReader = myComm.ExecuteReader();
while (dataReader.Read())
{ totalRows++; }
dataReader.Close();
return totalRows;
}
public void GetTrackData(int TrackId) //returns info about track using key
{
SqlCommand myComm = new SqlCommand("SELECT * FROM Tracks WHERE TrackID = " + TrackId.ToString(), myConnection2);
SqlDataReader dataReader;
dataReader = myComm.ExecuteReader();
dataReader.Read();
td.SetTrackData(dataReader[0].ToString(),dataReader[1].ToString(),dataReader[2].ToString());
dataReader.Close();
}
现在,connection1正确存在,但是当我在客户端中调用 GetTrackData(trackId)时,它说 dataReader 中没有数据.可能是什么原因? thnx
Now connection1 is owrking properly but when I call GetTrackData(trackId) in my client, it says that no data is present in the dataReader. What can be the reason? thnx
推荐答案
public int GetTotalRows()//returns number of (already distinct) records
{
SqlCommand myComm = new SqlCommand("SELECT COUNT(*) FROM Tracks", myConnection2);
int totalRows = 0;
SqlDataReader dataReader;
dataReader = myComm.ExecuteReader();
while (dataReader.Read())
{
totalRows++;
}
dataReader.Close();
return totalRows;
}
返回1.
使用totalRows = Convert.ToInt32(SqlCommand.ExecuteScalar());
查询返回行数.
GetTrackData:
Returns 1.
Use totalRows = Convert.ToInt32( SqlCommand.ExecuteScalar() );
The query returns the number of rows.
GetTrackData:
if(dataReader.Read()) // Check that you actually got a result
{
// I''d rather use the GetXXX methods
td.SetTrackData(dataReader.GetString(0),
dataReader.GetString(1),
dataReader.GetString(2));
}
问候
Espen Harlinn
Regards
Espen Harlinn
GetTotalRows
方法和GetTrackData
方法都使用相同的SqlConnection object
.我确定其中之一应该使用myConnection1
代替.
Both theGetTotalRows
method and theGetTrackData
method use the sameSqlConnection object
. I''m sure one of them should usemyConnection1
instead.
这几乎是一种是否插入"答案,但我认为需要提出这一问题.
当您调用
This is pretty much an "is it plugged in" type of answer, but I think it needs to be asked.
When you call
public void GetTrackData(int TrackId)
在测试过程中,您是否确定轨道"表中的记录确实存在该值?
while testing have you ensured that a record in the Tracks table actually exists with that value?
这篇关于类库无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!