问题是:
对象没有实例化,
我调试跟踪后,发现 数据库连接的状态在
conn.Open()之前已经是Open了--->这个问题没有想通,因为之前并没有对数据任何操作啊!
其次, 我的string[] result = new string[20];这样定义的
但是这样肯定不好,毕竟result的大小是不确定的, 那么应该如何定义呢?
代码如下:
private string[] RetrieveFileFromDatabase(int r, int g, int b)
{
SqlConnection conn = null;
SqlCommand cmd = null;
SqlParameter FileName = null;
SqlParameter Rvalue = null;
SqlParameter Gvalue = null;
SqlParameter Bvalue = null;
SqlDataReader dr = null;
string[] result = new string[20];

try
{
//
// connect to DB
//
conn = new SqlConnection(ConnectionString());
cmd = new SqlCommand("RetrieveFile", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
// ---------------------------------------------- //
// Initializing parameters and assigning the values to be sent to the server
//
FileName = new SqlParameter("@FileName",SqlDbType.VarChar,250);
FileName.Direction = ParameterDirection.Output; 
Rvalue = new SqlParameter("@Rvalue",System.Data.SqlDbType.Int);
Rvalue.Value = r;
Gvalue = new SqlParameter("@Gvalue",System.Data.SqlDbType.Int);
Gvalue.Value = g;
Bvalue = new SqlParameter("@Bvalue",System.Data.SqlDbType.Int);
Bvalue.Value = b;
// ----------------------------------------------

//
//Adding the parameters to the database. Remember that the order in which the parameters are added is VERY important!
//
cmd.Parameters.Add(FileName);
cmd.Parameters.Add(Rvalue);
cmd.Parameters.Add(Gvalue);
cmd.Parameters.Add(Bvalue);
// ---------------------------------------------- //
// Opening the connection and executing the command.
//  The idea behind using a dataReader is that, on the SQL Server, we cannot assign to a
// variable the value of an image field. So, we use a querry to select the record we want 
// and we use a datareader to read that query.
// 
conn.Open();
dr = cmd.ExecuteReader();
int i = 0;
while(dr.Read())
{
result[i] = (string)dr.GetValue(0);
i++;
} //
// Closing the datareader and the connection
//
dr.Close();
conn.Close();
// ------------------------------------------ //
// Disposing of the objects so we don't occupy memory.
//
conn.Dispose();
cmd.Dispose();
// ------------------------------------------ }
catch(Exception e)
{
//
// If an error occurs, we assign null to the result and display the error to the user,
// with information about the StackTrace for debugging purposes.
//
Console.WriteLine(e.Message + " - " + e.StackTrace);
result = null;
} return result;
}存储过程如下:/*create RetrieveFile store-procedure*/
ALTER PROCEDURE dbo.RetrieveFile
(
@Rvalue integer,
@Gvalue integer,
@Bvalue integer,
@FileName varchar(250) OUTPUT
)
AS
select FileName
from Pictures
where Rvalue = @Rvalue
and Gvalue = @Gvalue
and Bvalue = @Bvalue

解决方案 »

  1.   

    我调试跟踪后,发现 数据库连接的状态在
    conn.Open()之前已经是Open了--->这个问题没有想通,因为之前并没有对数据任何操作啊!你在页面上使用了适配器,适配器会自己打开连接,而且是在“不知道什么时候”打开的,反正它是保持好它要用的时候是开的,至于什么时候开就不知道了。所以在使用适配器的地方,打开连接前要先判断状态(养成任何地方打开前先判断是好习惯)
      

  2.   

    to:  syeerzy(快乐永远) 
    谢谢你的建议!
      

  3.   

    string[] result = new string[20];
    这样不好,可以考虑使用ArrayList