我正在创建一个应该与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();
}
}
如果我的一个表中有“priemer”列,这应该是chceck。如果是,它应该结束/中断,如果不是,我的表应该用新列改名,名为“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()
答案 0 :(得分:1)
因此,如果我理解正确,如果第一个字段未命名为priemer
,则会创建一个新列以尝试关闭阅读器。但是你不能停止你的循环,因此你会在阅读器关闭的情况下执行第二个循环。
有一种更好的方法可以确定您的表是否包含所需的列
bool found = false;
using(MySqlConnection cnn = new MySqlConnection("....................."))
{
cnn.Open();
DataTable dt = cnn.GetSchema("COLUMNS", new string[] {null,null,"feture", null});
foreach(DataRow r in dt.Rows)
{
if(r["COLUMN_NAME"].ToString() == "priemer")
{
found = true;
break;
}
}
if(!found)
{
.. create your column here
}
}
答案 1 :(得分:1)
根据以下讨论,您可以运行此查询:
SELECT COUNT(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'feture'
AND COLUMN_NAME = 'premier'
如果你得到CNT = 0
(在结果集的第一个单元格中),那么你必须运行你的ALTER TABLE查询,否则它已经存在。仍然不需要运行for循环。