gpt4 book ai didi

c# - 未将对象引用设置为对象实例 - MySqlCommand

转载 作者:行者123 更新时间:2023-11-29 08:04:16 26 4
gpt4 key购买 nike

我正在制作一个应与 MySql 数据库一起使用的应用程序。我收到此错误,但我不知道为什么:

Object reference not set to an instance of an object

这是我的代码:

public void rtMean()
{
MySqlConnection conect = con.ConnectToMySQL();
MySqlDataReader reader;
MySqlDataReader readerMean;

// OVERENIE, CI UZ STLPEC PRIEMER EXISTUJE

string query1 = "SELECT * FROM feture";
MySqlCommand cmd1 = new MySqlCommand(query1, conect);
reader = cmd1.ExecuteReader();

for (int j=0; j < reader.FieldCount; j++)
{
if (reader.GetName(j) == "priemer")
{
break;
}
else
{
reader.Close();
cmd1.Cancel();
string queryAlterTable = "ALTER TABLE feture ADD COLUMN priemer FLOAT NOT NULL";
MySqlCommand cmd = new MySqlCommand(queryAlterTable, conect); //here I am getting the error
cmd.ExecuteScalar();
}
}

这应该检查我的一个表中是否有“primer”列。如果是,它应该结束/中断,如果不是,我的表应该用名为“priemer”的新列进行更改。

感谢您的建议。

编辑:堆栈 - 跟踪:

  at MySql.Data.MySqlClient.MySqlCommand.ExecuteScalar()
at WindowsFormsApplication1.ClusteringReady.rtMean() in c:\Users\Martin\Desktop\CLUSTER\WindowsFormsApplication1\ClusteringReady.cs:line 43
at WindowsFormsApplication1.Form1.clusterize_Click(Object sender, EventArgs e) in c:\Users\Martin\Desktop\CLUSTER\WindowsFormsApplication1\Form1.cs:line 130
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in c:\Users\Martin\Desktop\CLUSTER\WindowsFormsApplication1\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

最佳答案

好的,根据下面的讨论,您可以运行此查询:

SELECT COUNT(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'feture'
AND COLUMN_NAME = 'premier'

如果您得到CNT = 0(在结果集的第一个单元格中),那么您必须运行您的 ALTER TABLE 查询,否则它已经存在。仍然不需要运行 for 循环。

关于c# - 未将对象引用设置为对象实例 - MySqlCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23048509/

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