将C#中的DataGrid绑定到MySQL-MySQL连接问题
本文关键字:MySQL-MySQL 连接 问题 绑定 中的 DataGrid | 更新日期: 2023-09-27 18:02:16
我有一个简单的程序,试图在WPF/C#应用程序中显示MySQL表。现在,由于某种原因,我的连接似乎有问题,我不太清楚是什么。
我的主窗口:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Szuper adatbázisfeltöltő" Height="267.143" Width="525">
<Grid>
<TabControl HorizontalAlignment="Left" Height="236" VerticalAlignment="Top" Width="517">
<TabItem Header="Adatbázis">
<Grid Background="#FFE5E5E5">
<DataGrid x:Name="dataGrid1" ItemsSource="{Binding}" AutoGenerateColumns="True" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Height="119" Width="441"/>
<Button Content="Új rekord" HorizontalAlignment="Left" Margin="0,134,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Frissítés" HorizontalAlignment="Left" Margin="80,134,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Törlés" HorizontalAlignment="Left" Margin="160,134,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</TabItem>
<TabItem Header="Adminisztráció">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
</Window>
当我运行程序时会发生什么:
public MainWindow()
{
InitializeComponent();
UpdateGrid();
}
public void UpdateGrid()
{
// dataGrid1.DataContext = Class1.getTable().DefaultView;
//dataGrid1.SelectedValuePath = "index";
}
}
神奇的地方应该发生:
class Class1
{
static String str = "Server=http://mysql10.000webhost.com;Database=a2116591_adat;Uid=a2116591_tkis;Pwd=zseton22";
static MySqlConnection con = null;
public static DataTable getTable()
{
//MySqlDataReader Object
MySqlDataReader reader = null;
try
{
con = new MySqlConnection(str);
con.Open(); //open the connection
//We will need to SELECT all or some columns in the table
//via this command
String cmdText = "SELECT * FROM adat";
MySqlCommand cmd = new MySqlCommand(cmdText, con);
reader = cmd.ExecuteReader(); //execute the reader
DataTable dt = new DataTable();
dt.Load(reader);
return dt;
}
catch (MySqlException err)
{
Console.WriteLine("Error: " + err.ToString());
return null;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (con != null)
{
con.Close(); //close the connection
}
}
}
}
}
我试着改变连接字符串,但没有用,我有点卡住了。
class Class1
{
static String str = "your connection string";
static MySqlConnection con = null;
public static DataSet getTable()
{
//MySqlDataReader Object
MySqlDataReader reader = null;
try
{
con = new MySqlConnection(str);
con.Open(); //open the connection
//We will need to SELECT all or some columns in the table
//via this command
String cmdText = "SELECT * FROM adat";
MySqlCommand cmd = new MySqlCommand(cmdText, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "R");
con.Close();
return ds;
}
catch (MySqlException err)
{
Console.WriteLine("Error: " + err.ToString());
return null;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (con != null)
{
con.Close(); //close the connection
}
}
}
}
}
public MainWindow()
{
InitializeComponent();
UpdateGrid();
}
public void UpdateGrid()
{
dataGrid1.DataMember = "R";
dataGrid1.DataSource = class1.gettable();
}