无法从"string"转换为"System.Data.SqlClient.SqlCommand”

TerraRestore 2021-10-15 22:14:11

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;     // 引用配置文件
using System.Data.SqlClient;    // 引用数据库操作文件

namespace 大家杯
{
    public partial class 凭证 : Form
    {
        public string pzh;   // 定义全局变量:凭证号
        public DateTime rq;   // 定义全局变量:日期

        public 凭证()
        {
            InitializeComponent();
        }

        private void 凭证_Load(object sender, EventArgs e)
        {
            if (rq != null && pzh != null)
            {
                t_pzh.Text = pzh;
                t_rq.Text = rq.ToString();
                SearchPz();
            }
        }
        private void SearchPz()
        {
            // 链接数据库
            string ConString = ConfigurationSettings.AppSettings["dajiebeidb"];
            SqlConnection conn = new SqlConnection(ConString);
            conn.Open();

            // 从数据库中查找凭证明细信息(数据来自于凭证明细表:ZW_PZMXB)   
            SqlDataAdapter da = new SqlDataAdapter("select zy as 摘要,jfje as 借方金额,dfje as 贷方金额,km_name as 会计科目 from zw_pzzb as a,zw_pzmxb as b,zw_kjkmbmb as c where b.KM_CODE=c.KM_CODE and a.nkjqj=b.nkjqj and a.ykjqj=b.ykjqj and a.pzh=b.pzh and b.nkjqj='" + rq.Year + "' and b.ykjqj='" + rq.Month.ToString().PadLeft(2, '0'); +"' and a.pzh='" + pzh + "';", conn);

            // 凭证明细信息绑定到 DataGridView 中
            DataSet dt = new DataSet();
            da.Fill(dt, "pz");
            dgv_pzmx.DataSource = dt.Tables[0];

            //从数据库中查找凭证主表信息(数据来自于凭证主表:ZW_PZZB) 
            SqlCommand cmd = new SqlCommand("select fdjs,zdr,shr,jzr from zw_pzzb where rq='" + rq + "' and pzh='" + pzh + "';", conn);
            SqlDataReader reader = cmd.ExecuteReader();//返回只能读取,不能写入的SqlDataReader对象
            while (reader.Read())
            {
                t_fdjs.Text = reader["fdjs"].ToString();
                t_zdr.Text = reader["zdr"].ToString();
                t_shr.Text = reader["shr"].ToString();
                t_jzr.Text = reader["jzr"].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pzh = t_pzh.Text;
            rq = t_rq.Text;

            SearchPz();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
    }

...全文
683 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sharksoftster 2021-10-25
  • 打赏
  • 举报
回复

说实话,你都能把代码贴出来了,为什么不实用DEBUG模式跟踪下看看问题出在哪里呢?

shoppo0505 2021-10-25
  • 打赏
  • 举报
回复

你可以先在debug模式,先把要执行的sql脚本解析出来,然后在数据库里面执行一下,看是不是正确。
你现在da.Fill(dt, "pz");这里的语法不对。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧