我正在尝试创建一个可以访问数据库,加载数据以及用户是否添加任何新字段的应用程序,以将数据保存回数据库。目前,我可以将数据加载到应用程序中,但由于上面给出的错误,我仍然无法保存任何更改。
这是保存方法
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);
}
}
}
答案 0 :(得分:3)
您已将//Checking user's city
__block NSString *userCity;
[[GMSGeocoder geocoder]reverseGeocodeCoordinate:self.locationManager.location.coordinate completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
if (error) {
NSLog(@"%@",[error description]);
}
userCity=[[[response results] firstObject] locality];
}];
//Checking store's city
__block NSString *storeCity;
[[GMSGeocoder geocoder]reverseGeocodeCoordinate:arounder.radiusCircularRegion.center completionHandler:^(GMSReverseGeocodeResponse *response, NSError *error) {
if (error) {
NSLog(@"%@",[error description]);
}
arounderCity=[[[response results] firstObject] locality];
}];
if ([userCity isEqualToString:arounderCity]) {
return YES;
}
声明为updateDataSet
。
静态意味着您在Type上调用它,而不是该类型的实例(如错误所示)。
不是这样:public static void updateDataSet(DataSet dataSet)
但是:dataSource.updateDataSet(dataSet);
编辑:你还应该将SimpleDataSource.updateDataSet(dataSet);
中的SimpleDataSource
方法重命名为其他方法,否则编译器会认为你的意思是指方法而不是类。