如何解决CommandText属性尚未初始化?

编程入门 行业动态 更新时间:2024-10-20 06:24:30
本文介绍了如何解决CommandText属性尚未初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨我收到此错误" CommandText属性尚未初始化"

代码如下:

private DataTable GetData(string p) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings [" ApplicationServices"]。ConnectionString; 使用(SqlConnection con = new SqlConnection(constr)) { using(SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query)) { 使用(SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); }¥b $ b }¥b $ b 返回dt; } b $ b}

任何人都知道帮助我解决问题。

Divya Diya

解决方案

异常消息提示您在调用Fill方法之前需要将SELECT查询分配给cmd.CommandText属性。确保查询变量的值不为空或为空。

hi am getting this error "CommandText property has not been initialized"

The code is below:

private DataTable GetData(string p) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); } } return dt; } }

Anyone Know help me to solve the issue.

Divya Diya

解决方案

The exception message is giving you a hint that a SELECT query needs to be assigned to the cmd.CommandText property before invoking the Fill method. Make sure the value of the query variable is not null or empty.

更多推荐

如何解决CommandText属性尚未初始化?

本文发布于:2023-10-25 08:41:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   如何解决   属性   CommandText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!