问题是:
对象没有实例化,
我调试跟踪后,发现 数据库连接的状态在
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
对象没有实例化,
我调试跟踪后,发现 数据库连接的状态在
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
解决方案 »
- 求解释下什么意思
- 怎样让c# winform程序单击显示在最上层
- 急求解答(右键问题)
- .net里用user control的 pageload先执行还是被调用的函数先执行?
- 不想反复连接数据库,如何避免,得到正解立即结贴
- com在不同平台不能调用。
- C#里面有没有像delphi、c++ builder里面的tcpServer的控件?
- 如何将一个HTML 文件提交到C#.NET里面
- 奇怪现象: 程序最大化后,快速拖动分割条,图片有时候不能正常显示.... 简单程序,看原程序。
- 系统在运行多个EXCEL进程,我想结束一个指定的EXCEL进程但不影响其它的进程,该怎么办?
- c#中,请有现成的关于远程对象的代码
- 如何在windows的dos环境下执行命令
conn.Open()之前已经是Open了--->这个问题没有想通,因为之前并没有对数据任何操作啊!你在页面上使用了适配器,适配器会自己打开连接,而且是在“不知道什么时候”打开的,反正它是保持好它要用的时候是开的,至于什么时候开就不知道了。所以在使用适配器的地方,打开连接前要先判断状态(养成任何地方打开前先判断是好习惯)
谢谢你的建议!
这样不好,可以考虑使用ArrayList