string constr = "server=localhost;Database=unity3d;User Id=root;password=root";
//建立连接的语句
//如果是本地数据库server为localhost,不是则输入server的地址
MySqlConnection mycon = new MySqlConnection(constr); //建立连接
mycon.Open();
string selstr1 = "select fengsu, kw from sudu ";
MySqlCommand myselect1 = new MySqlCommand(selstr1, mycon);
DataSet ds1 = new DataSet();
try
{
MySqlDataAdapter da1 = new MySqlDataAdapter(selstr1, mycon);
da1.Fill(ds1);
}
catch (Exception ee)
{
throw new Exception("SQL: " + selstr1 + "\n" + ee.Message.ToString());
}
mycon.Close();
for(int i=0;i<100;i++){
float x = float.Parse(ds1.Tables[0].Rows[i][0].ToString());
float y = float.Parse(ds1.Tables[0].Rows[i][1].ToString());
myWMGSeries.pointValues.Add(new Vector2(x,y));
}
这段代码展示了如何使用C#连接到MySQL数据库,执行查询语句并获取数据。通过MySqlConnection和MySqlDataAdapter,从'sudu'表中选取'fengsu'和'kw'字段,并将结果转换为浮点数存储到WMGSeries的pointValues中。
3896

被折叠的 条评论
为什么被折叠?



