- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个应与 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/
我试图验证当 mysql 执行非查询时,它应该返回受影响的行。然而,当我使用存储过程时,它总是返回 0。 注意:此代码是通用的,用于执行任何存储过程并在必要时返回存储过程的输出值。 C#: /
我正在构建一个函数来处理命令,但 MySQLCommand 没有将我的字符串作为以下代码的参数,您能帮助我吗? int executeCommand(const std::string & mySql
我在 C# 中遇到 MySqlParamters 问题。我将在数据库中创建一个新条目,但 MySqlCommand 只写入空值。 MySqlCommand see = new MySqlCom
我有一个查询在 MySql 工作台中运行良好,但产生语法错误 “您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以了解在附近使用的正确语法':= 0+1 AS Rank,
我正在制作一个应与 MySql 数据库一起使用的应用程序。我收到此错误,但我不知道为什么: Object reference not set to an instance of an object 这
下面的代码似乎没有将参数值插入到查询中(查询没有返回任何内容)。如果我在数据库中测试该查询(当然我输入传递的值而不是 ?author 参数并返回一些行。为什么? var conn = new M
我正在使用 MySqlCommand 在我的 vb.net 应用程序中执行查询插入,现在我有这个查询: UPDATE primo_appointments SET book_datetime =
我正在尝试运行带有参数的 MySqlCommand,但是当我查看 SQL 日志时,该参数出现了。 我尝试过使用 @ 和 ?,也尝试过在我的 sqlconnection 字符串中使用“old synta
我正在创建一种通过传递搜索字段从任何表中选择 ID 的方法。 private int SelectId(string tabela, string campo, string valor) {
在以下代码中,用于获取特定行中的产品列表,该命令仅在我将 productLine 硬编码(连接)到 SQL 中时才返回结果。参数替换永远不会发生。 + "lineName =
我一直在尝试不同的方法来让我的代码正常工作。 我对 C# 和 DB 都很陌生。但问题是我正在尝试为我添加乐队名称和成员的音乐事件修复数据库。 我有 2 个表。一个和乐队在一起,一个和乐队成员在一起。我
我试图在 mysql 数据库中执行 INSERT 查询,但除了代码执行停止并且没有插入任何内容之外,什么也没有发生。这是代码(连接是在另一点建立的并且正在工作): query
我不知道如何使用 Connector/NET 3.6.5 提供的 MySQLCommand。基本上这里是我正在使用的代码和我在运行 ExecuteNonQuery 时得到的异常。我在这里做错了什么?我
代码如下: MySqlCommand cmd = new MySqlCommand( "SELECT * FROM DB_name_here WHERE some_field =@some_v
当我运行以下代码时: query = "select count(*) from table where name = '?name'"; MySqlConnection connection =
在我的一个项目中,我必须从数据库中获取最后插入的 ID,这样我就可以将新插入的数据与内存中的数据同步,而无需执行 SELECT * FROM my_db; 查询,即我我正在执行 SELECT * FR
我有一个 MySql 数据库,里面有九个表。我需要从 C# 中的异步作业动态上传到其中六个。但是为了加快上传速度,其中四个使用MySqlBulkLoader,另外两个使用手动上传的方式。为了支持并发并
我正在尝试使用以下代码插入到 mysql 表中: // open connection here this.conn.Open(); // fields from post request var e
我有一个存储过程,如果没有错误,它可以返回 2 组结果。 第一组只是一个普通的select。 第二组包含错误代码和错误信息,这一组总是返回一行。由于没有错误,错误代码将为 0,错误消息将为空。 和 如
我发现在我的每个查询(需要自己的命令或写入 parameters.clear())上编写 using 语句非常烦人,有时需要在 using block 之外声明变量。与没有处理对象的版本相比,它非常烦
我是一名优秀的程序员,十分优秀!