数据库中有数据,但未返回任何值。我想从数据库中检索值
我已经完成插入,它可以正常工作,但是选择不起作用
public class Portal
{
public SqlCommand cmd = null;
ConnectionClass obj = new ConnectionClass();
public ArrayList fetchEmployee(int empid, String fname, String lname, String qualification, String dob, int status, String image, String statusname, String path)
{
ArrayList ar = null;
SqlDataReader dr = null;
try
{
obj.getConnection();
cmd = new SqlCommand("PROC_EMPLOYEE",obj.con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@flag", SqlDbType.Int).Value = status;
cmd.Parameters.Add("@statusname", SqlDbType.VarChar).Value = statusname;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = empid;
cmd.Parameters.Add("@fname", SqlDbType.VarChar).Value = fname;
cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = lname;
cmd.Parameters.Add("@qualification", SqlDbType.VarChar).Value = qualification;
cmd.Parameters.Add("@dob", SqlDbType.VarChar).Value = dob;
cmd.Parameters.Add("@image", SqlDbType.NVarChar).Value = image;
cmd.Parameters.Add("@path", SqlDbType.NVarChar).Value = path;
obj.con.Open();
dr = cmd.ExecuteReader();
while(dr.Read()){
ar=new ArrayList();
ar.Add(dr[1].ToString());
Console.WriteLine(dr["fname"].ToString());
}
dr.Close();
obj.con.Close();
}
catch(Exception ex) {
ar=null;
}
}
}
}
Here is my procedure
ALTER PROCEDURE [dbo].[PROC_EMPLOYEE](@id numeric(18,0), @fname varchar(25) ,@lname varchar(25), @dob varchar(25),@statusname varchar(25) ,@qualification varchar(25),@image nvarchar(25) = NULL,@flag int,@path nvarchar(MAX) = NULL)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare
@statusid numeric(18,0),
@i numeric(18,0),
@flag1 int
-- Insert statements for procedure here
set @flag1=@flag;
if(@flag1 = 1)
BEGIN
Set @statusid = (Select status_id from dbo.Status where status_name = @statusname);
Insert into Employee(fname,lname,qualification,dob,image,status_id,path) values(@fname,@lname,@qualification,@dob,@image,@statusid,@path);
END
if(@flag1 = 2)
BEGIN
Delete from Employee where emp_id=@id;
END
if(@flag1 = 3)
BEGIN
Select fname from Employee where emp_id=@id;
END
if(@flag1 = 5)
BEGIN
Select emp_id,fname,lname,qualification,dob from Employee;
END
if(@flag1 = 4)
BEGIN
Set @statusid = (Select status_id from dbo.Status where status_name = @statusname);
update Employee set qualification=@qualification, image=@image, dob=@dob,fname=@fname,lname=@lname,status_id=@statusid where emp_id=@id;
END
END
我希望它获取记录,但返回的记录未找到。它已正确连接到数据库。有人可以帮忙吗
答案 0 :(得分:0)
您应该对catch做一些有用的事情(异常例外){...} 当前ex.Message可能返回:“ ExecuteReader:连接属性尚未初始化。” 您应该为cmd.Connection分配要使用的连接的值。
(请参阅:SQL Server error: ExecuteNonQuery: Connection property has not been initialized)