Reputation: 151
I am making an application witch uses a Browser to show a viral video a week.The url for the video is stored in a my MySQL database online along with an int that shows a counter in the top right hand corner, What I want to do is get the string of text from the MySQL data base and the in and write that information into a text box called Video1 and Counter.
Here is some of my current code:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = Convert.ToInt32(Refresh_Rate.Text);
timer1.Enabled = true;
try
{
string myConnrction = "datasource=************;port=3306;username=*******;password=*******";
MySqlConnection myConn = new MySqlConnection(myConnrction);
MySqlCommand SelectCommand = new MySqlCommand("select * from sql337069.Youtube where Video1='", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
while (myReader.Read())
{
}
MySqlDataAdapter myDataAdapter =new MySqlDataAdapter();
myDataAdapter.SelectCommand = new MySqlCommand(" select * sql337069.Youtube ;", myConn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
myConn.Open();
DataSet ds = new DataSet();
MessageBox.Show("Working");
myConn.Close();
}
catch
{
MessageBox.Show("Null");
}
So I guess I am looking for is something like Video1.text += [?]
.
Upvotes: 0
Views: 677
Reputation: 151
For Me i Have discovered that this method works best But thanks to Kevin for helping me fill in some code.
try
{
//Defanitions
string myConnrction = "*********;port=3306;username=******;password=******";
MySqlConnection myConn = new MySqlConnection(myConnrction);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.SelectCommand = new MySqlCommand(" select * sql337069.Youtube;", myConn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
DataSet ds = new DataSet();
string Query = "select * from sql337069.Youtube ;";
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
MySqlDataReader myreader;
//Defanitions
myConn.Open();
myreader = cmdDataBase.ExecuteReader();
while (myreader.Read())
{
string sName = myreader.GetString("Video2_Refresh");
Video2_Refresh.Text = sName;
}
MessageBox.Show("Working");
}
catch
{
MessageBox.Show("Null");
}
(I am posting this for stack overflow reference Because i could not find much on this subject )
Upvotes: 0
Reputation: 160
From what I can see, it would be better for you to grab the columns that you want in your select statement, rather than selecting all column (*). When you select the ones you want, you know the exact structure of the data that is being returned and you can hold that in a class or structure that you create. say, something like this for example:
public struct Video
{
public int ID;
public string Name;
public int Count;
}
static void Main(string[] args)
{
string myConnectionString = "YOUR_CONNECTION_STRING";
SqlConnection myConn = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand("SELECT ID, VideoName, VideoPlayCount FROM dbo.VideoTable WHERE Id = YOUR_VIDEO_RECORD_ID", myConn);
Video video = new Video();
myCommand.CommandType = System.Data.CommandType.Text;
myConn.Open();
SqlDataReader reader = myCommand.ExecuteReader(CommandBehavior.SingleResult);
reader.Read();
video.ID = reader.GetInt32(0);
video.Name = reader.GetString(1);
video.Count = reader.GetInt32(2);
myConn.Close();
string myTextBoxValue = video.Name + " " + video.Count.ToString();
}
Using this example would require you to replace the "YOUR_CONNECTION_STRING" with the connection string to your database, as well as entering in the correct table name and table column for the select statement, but other than that, this should get you what you are looking for I believe. The string "myTextBoxValue" is being assigned the video name and count as requested.
Hope this helps.
Upvotes: 1