我正在制作一个应用程序,女巫使用浏览器每周显示一个病毒视频。视频的 URL 存储在我的 MySQL 在线数据库中,并带有一个在右上角显示计数器的 int,我是什么想要做的是从 MySQL 数据库获取文本字符串并将该信息写入名为 Video1 和 Counter 的文本框。
这是我当前的一些代码:
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");
}
所以我想我正在寻找类似 Video1.text += [?]
的内容。
最佳答案
据我所知,您最好在 select 语句中获取所需的列,而不是选择所有列 (*)。当您选择所需的数据时,您就知道要返回的数据的确切结构,并且可以将其保存在您创建的类或结构中。比如说,像这样的事情:
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();
}
使用此示例需要您将“YOUR_CONNECTION_STRING”替换为数据库的连接字符串,并为 select 语句输入正确的表名称和表列,但除此之外,这应该会为您提供什么我相信你正在寻找。字符串“myTextBoxValue”被分配视频名称并根据请求进行计数。
希望这有帮助。
关于c# - 获取 Mysql 中的 Text 和 int 表单并存储在文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155569/