我正在尝试创建一个可以访问数据库,加载数据的应用程序,如果用户添加了任何新字段,则将数据保存回数据库中。截至目前,我已经能够将数据加载到应用程序中,但是由于上述错误,我仍然无法保存任何更改。
这是保存方法
private void Save_Click(object sender, RoutedEventArgs e)
{
dataSource.updateDataSet(dataSet);
}
这是SimeDataSource文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;
class SimpleDataSource : IDisposable
{
MySqlConnection conn;
MySqlDataAdapter adapter = new MySqlDataAdapter();
public SimpleDataSource(string server, string database, int port,
string user, string password)
{
Connect(server, database, port, user, password);
}
public void Connect(string server, string database, int port,
string user, string password)
{
string connectionString = "server=" + server + ";database=" + database + ";port=" + port + ";user=" + user + ";password=" + password;
try
{
conn = new MySqlConnection(connectionString);
conn.Open();
}
catch (MySqlException e)
{
Console.WriteLine("Error: {0}", e.ToString());
}
finally
{
if (conn == null)
{
conn.Close();
}
}
}
public MySqlDataReader Query(string queryString)
{
// TODO: Declares MySqlDataReader and MySqlCommand objects.
// When the MySqlCommand object is executed with the query
// string, the return value will be assigned to the MySqlDataReader
// object. This object is then returned with the "return" keyword.
try
{
MySqlCommand cmd = new MySqlCommand(queryString, conn);
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
Console.WriteLine("Error: {0}", e.ToString());
}
return null;
}
public void Update(string updateString)
{
try
{
MySqlCommand cmd = new MySqlCommand(updateString, conn);
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
Console.WriteLine("Error: {0}", e.ToString());
}
}
public void Dispose()
{
if (conn != null)
conn.Dispose();
}
public void QueryPreparedStatement(string queryString, Dictionary<string, string> values)
{
try
{
MySqlCommand cmd = new MySqlCommand(queryString, conn);
foreach (string key in values.Keys)
{
cmd.Parameters.AddWithValue("@" + key, values[key]);
}
cmd.ExecuteNonQuery();
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
}
}
public DataTable DataTableQuery(string sqlQuery)
{
DataTable dt = new DataTable();
MySqlCommand cmd = new MySqlCommand(sqlQuery, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
adapter.SelectCommand = cmd;
try
{
adapter.Fill(dt);
return dt;
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
return null;
}
}
public static void updateDataSet(DataSet dataSet)
{
try
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.Update(dataSet);
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
}
}
}
编辑:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Application_Development_Project
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private SimpleDataSource dataSource;
private DataSet dataSet;
public MainWindow()
{
InitializeComponent();
dataSource = new SimpleDataSource
("*****","*****",*****,"*****","*****");
}
public void SimpleDataSource(string server, string database, int port, string user, string password)
{
string connectionString = "server=" + server + ";database=" + database + ";port=" + port + ";user=" + user + ";password=" + password;
}
private void Load_Click(object sender, RoutedEventArgs e)
{
DataTable dt = dataSource.DataTableQuery("SELECT * FROM Company");
dataGrid.ItemsSource = dt.DefaultView;
}
private void Save_Click(object sender, RoutedEventArgs e)
{
SimpleDataSource.updateDataSet(dataSet);
}
}
}
最佳答案
您已将updateDataSet
声明为public static void updateDataSet(DataSet dataSet)
。
静态意味着您在Type上调用它,而不是该类型的实例(如错误所示)。
不是这个:dataSource.updateDataSet(dataSet);
但这是SimpleDataSource.updateDataSet(dataSet);
编辑:您还应该将SimpleDataSource
中的MainWindow
方法重命名为其他名称,否则编译器会认为您是指该方法而不是类。