Jump to content

so i have a table in the database with a column named finish that is declared with the datatype time.i want to call this stored time to my c# program.i have tried the following code and i have tried to display the variable value in a label to see what is the value in it but it seems that no value has been assigned to the label.it just displays label 3.i also want to separately extract the hours,minutes and seconds from the time in the database and put it to different int variables such as H,M and S for doing calculations. any ideas how this can be done?


 

                
               cn.ConnectionString = @".......";
            cmd.Connection = cn;

                cn.Open();
                string query5 = "select finish from activities where act_ID ='2'";
                SqlCommand query5 = new SqlCommand(query5, cn);
                SqlDataReader reader5 = query5.ExecuteReader();
                reader5.Read();
                String ftime = reader5["finish"].ToString();
                reader5.Close();
                cn.Close();


                label3.Text = ftime;

 

Link to post
Share on other sites

using MySql.Data.MySqlClient;

MySqlConnection conn = new MySqlConnection("SERVER=127.0.0.1;DATABASE=database;UID=root;PASSWORD=")


conn.Open(); 
string query = "SQL query here"); 
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader dataread = cmd.ExecuteReader();
if (dataread.HasRows) 
{ 
while(dataread.Read())
// dataread['ID'] would be would be the column ID in the database
//here I built up an XML string to return as this was for a web service.
}
conn.Close(); 

This is how I did it in university last year.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

9 minutes ago, vorticalbox said:

using MySql.Data.MySqlClient;

MySqlConnection conn = new MySqlConnection("SERVER=127.0.0.1;DATABASE=database;UID=root;PASSWORD=")


conn.Open(); 
string query = "SQL query here"); 
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader dataread = cmd.ExecuteReader();
if (dataread.HasRows) 
{ 
while(dataread.Read())
// dataread['ID'] would be would be the column ID in the database
//here I built up an XML string to return as this was for a web service.
}
conn.Close(); 

This is how I did it in university last year.

i read your query several times and i find it difficult to understand it.we havent done the lesson web services yet so im not familiar to it.it would be helpful if u could simplify it a bit if theres no trouble.

Link to post
Share on other sites

18 minutes ago, Shammikit said:

i read your query several times and i find it difficult to understand it.we havent done the lesson web services yet so im not familiar to it.it would be helpful if u could simplify it a bit if theres no trouble.

The web services bit doesn't really matter but i will explain my code for you.

using MySql.Data.MySqlClient;

this tells VS what libraries we will be using

MySqlConnection conn = new MySqlConnection("SERVER=127.0.0.1;DATABASE=database;UID=root;PASSWORD=")

this is the connection information for the MySql server

conn.Open(); 

Open a connection to the database using the above details.

string query = "SQL query here"; 

This is your query such as SELECT * FROM Posts

MySqlCommand cmd = new MySqlCommand(query, conn);

Executes the query to the connection we gave

MySqlDataReader dataread = cmd.ExecuteReader();

This loads the data that is returned form the database into a variable called dataread 

if (dataread.HasRows)

Checks if we actually got rows of data.

while(dataread.Read())

So this is a loop that loops through every row that is returned so we can actually do stuff with it. In my case I created an XML string that was returned but you could do anything such as print to the console (if a console application), add the results to a textbox, listname even msgbox.show() if you're so inclined.

 

ID  Name  Colour
3   u1    blue
4   u2    red

 

while(dataread.Read())
{
	Console.Writeline(String.Format("ID {0} : Name {1} : Colour {3}", dataread['ID'], dataread['Name'], dataread['Colour']))
}

Would print out 

ID 3 : Name u1 : Colour blue
ID 4 : Name u2 : Colour red

To the console.

conn.Close(); 

Simple closes the connection.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×