gpt4 book ai didi

c# - "Cannot be accessed with an instance reference; qualify it with a type name instead"

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:21 24 4
gpt4 key购买 nike

<分区>

我正在尝试创建一个可以访问数据库、加载数据的应用程序,如果用户添加任何新字段,则将数据保存回数据库。截至目前,我能够将数据加载到应用程序中,但由于上述错误,我仍然无法保存任何更改。

这是保存方法

    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);
}

}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com