字符串不包含参数的定义 [英] string does not contain a definition for Parameters

查看:138
本文介绍了字符串不包含参数的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我是C#的新手,但一直停留在此错误上.我要做的就是从用户那里获取信息并将其保存到数据库中.任何帮助深表感谢.在此先谢谢您.



Hello,
I am new to C# and am stuck on this error. All i want to do is get info from the user and save it to a database. Any help is much appreciated. Thanks in advanced.



Error   1   'string' does not contain a definition for 'Parameters' and no extension method 'Parameters' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)



这是代码



Here is the code

string pat = textBox1.Text.Trim();
            string res = textBox2.Text.Trim();
            
            if (@pat.Length == 0 || @res.Length == 0)
            {
                MessageBox.Show("Please enter something in the boxes.");
                return;
            }
            MessageBox.Show(@pat +" " + @res);
           
            SqlConnection cnn = new SqlConnection(OMSDB_Connection_String);
            string command = "Insert INTO OTM_unsubscriptions (dbPatCnt, dbRespCnt, Email, PracticeID) " +
                                 " VALUES (@pat,@res, 1, 1)";
            SqlCommand cmd = new SqlCommand(command, cnn);
            command.Parameters.Add(new SqlParameter(@pat, "valueOf_dbPatCnt"));
            command.Parameters.Add(new SqlParameter(@res, "valueOf_dbRespCnt"));
            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();

推荐答案

首先,为什么要在字符串变量上加上@? @仅用于SQL,不用于C#变量.您可能需要将代码更改为此.

First of all, why are you putting @ on your string variables? @ is only used on SQL and not C# variables. You might want to change your code to this.

string pat = textBox1.Text.Trim();
            string res = textBox2.Text.Trim();
            
            if (pat.Length == 0 || res.Length == 0)
            {
                MessageBox.Show("Please enter something in the boxes.");
                return;
            }
            MessageBox.Show(pat +" " + res);
           
            SqlConnection cnn = new SqlConnection(OMSDB_Connection_String);
            string command = "Insert INTO OTM_unsubscriptions (dbPatCnt, dbRespCnt, Email, PracticeID) " +
                                 " VALUES (@valueOf_dbPatCnt,@valueOf_dbRespCnt, 1, 1)";
            SqlCommand cmd = new SqlCommand(command, cnn);
            command.Parameters.Add(new SqlParameter("@valueOf_dbPatCnt", pat));
            command.Parameters.Add(new SqlParameter("@valueOf_dbRespCnt", res));
            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();



我只是删除了变量上的@并修复了添加参数的代码.看来您仍然对ADO.Net有点困惑.我建议您阅读更多.



I simply removed the @ on the variables and fixed the code where you added your parameters. It seems your still a little confused with ADO.Net. I suggest you read more.


这篇关于字符串不包含参数的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆