Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi in my project problem when i want read record by sql datareader

What I have tried:

using (SqlConnection mysqlConnection = new SqlConnection(this.database.getConnectionString()))
         {



             using (SqlCommand mySqlCommand = new SqlCommand("GetDboName", mysqlConnection))
             {
                 mySqlCommand.CommandType = CommandType.StoredProcedure;

                 mySqlCommand.Parameters.AddWithValue("@Imei", imei);
                 mysqlConnection.Open();




                 using (SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
                     {
                         if (!mySqlDataReader.HasRows)
                         {
                             throw new QueryException(1, "Tracker " + imei + " is not registered.");
                         }
                         else
                         {
                             mySqlDataReader.Read();







                         }
                     }


                 //mysqlConnection.Close();

             }

         }






declare @imei nvarchar(100)=869170031585095
DECLARE @DatabaseName NVARCHAR(MAX)=null, @SQL NVARCHAR(MAX);


select  @DatabaseName= DboName  from [CentralRelation].[dbo].[TrackingMaster] where Imei=869170031585095


SET @SQL = N'USE ' + QUOTENAME(@DatabaseName) +'  SELECT dbo.LastLogDevice.time, dbo.Carrier.carrierID, dbo.Carrier.carrierTypeFId, dbo.Carrier.companyFId, dbo.Carrier.orgFId, dbo.Carrier.fleetFid, dbo.Carrier.carrierName, dbo.Carrier.carreiernameonmap,
                         dbo.Carrier.deviceImei, dbo.Carrier.displayorhidenonmap, dbo.Carrier.zoneFId, dbo.Carrier.SimNumber, dbo.Carrier.Status, dbo.Carrier.PelackNumber, dbo.Carrier.apnName, dbo.Carrier.apnUserName,
                         dbo.Carrier.apnPassword, dbo.Carrier.createdOn, dbo.Carrier.expiresOn, dbo.Carrier.Lastedit ,(select   CompanyName  from [CentralAlcazer].[dbo].[Tracker] as t1 where t1.DeviceImei='+@imei+') as dboname
                           FROM dbo.Carrier INNER JOIN 
                          dbo.LastLogDevice ON dbo.Carrier.carrierID = dbo.LastLogDevice.carrierFId 
                          WHERE(dbo.Carrier.deviceImei ='+ @imei+') AND(dbo.Carrier.createdOn < GETDATE()) AND(dbo.Carrier.expiresOn > GETDATE())  AND(dbo.Carrier.Status = 1)';

--PRINT(@SQL);


EXECUTE(@SQL);
Posted
Updated 20-May-20 3:13am
Comments
F-ES Sitecore 20-May-20 8:24am    
What's your question?

So it returns no values - almost certainly that's because your WHERE clause finds no matches.

Test your SP in SSMS and see what it returns when you give it specific values. When you're happy that works then use the C# debugger to find out exactly what you are passing to SQL and compare that with the values that worked in SSMS.

We can't do any of that for you - we have no access to your data.
 
Share this answer
 
Comments
mohamad_ali 20-May-20 9:33am    
its work in ssms and pass the correct parameter, but not retern result of exexute string
You have to do something with what you are reading, place it in a variable for later usage, or output it directly, for example.
C#
if (mySqlDataReader.HasRows)
{
   mySqlDataReader.Read();
   string value = ((IDataRecord)mySqlDataReader)[0];
}
else
{
   // No record found. Act accordingly.
}

Edit: here's something which you could try (avoiding using AddWithValue() method to specify the parameter type explicitly):
C#
mySqlCommand.Parameters.Add("@imei", SqlDbType.NVarChar).Value = imei;

Moreover, there is a flaw in your stored procedure: you are not using the imei parameter to select the database name; therefore, the selected database name is static (since based on the static value 869170031585095), whereas the second query is using the parameter.
SQL
SELECT @DatabaseName= DboName  from [CentralRelation].[dbo].[TrackingMaster] WHERE Imei = @imei;
 
Share this answer
 
v2
Comments
mohamad_ali 20-May-20 9:35am    
result of HasRows is false, but query has result in ssms
phil.o 20-May-20 11:57am    
Please see my updated answer.
F-ES Sitecore 20-May-20 12:21pm    
try putting

SET NOCOUNT ON

at the top of your SP
mohamad_ali 21-May-20 5:04am    
thnx for your answer, my problem solved ,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900