Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我使用字段名作为参数,这在理论上应该有效。正确。第一个参数被解析为文字字符串。@KevinJensenPetersen尝试将字段名括在方括号中([FieldName]),这可能会起作用。第一个参数是字符串yes,我将其传递到查询中,以便在WHERE中_C#_Sql_Parameters_Error Handling_Type Conversion - Fatal编程技术网

C# 为什么我使用字段名作为参数,这在理论上应该有效。正确。第一个参数被解析为文字字符串。@KevinJensenPetersen尝试将字段名括在方括号中([FieldName]),这可能会起作用。第一个参数是字符串yes,我将其传递到查询中,以便在WHERE中

C# 为什么我使用字段名作为参数,这在理论上应该有效。正确。第一个参数被解析为文字字符串。@KevinJensenPetersen尝试将字段名括在方括号中([FieldName]),这可能会起作用。第一个参数是字符串yes,我将其传递到查询中,以便在WHERE中,c#,sql,parameters,error-handling,type-conversion,C#,Sql,Parameters,Error Handling,Type Conversion,为什么我使用字段名作为参数,这在理论上应该有效。正确。第一个参数被解析为文字字符串。@KevinJensenPetersen尝试将字段名括在方括号中([FieldName]),这可能会起作用。第一个参数是字符串yes,我将其传递到查询中,以便在WHERE中进行测试。我应该如何解决它?我不知道这是否适合您的需要,但您可以尝试使用字符串。在“where”子句中格式化您的firld名称,并将参数设置为实际值。这将使您无法对每个案例进行新的查询。我尝试的是采用灵活的方式进行SQL查询,而不是每次都要进行


为什么我使用字段名作为参数,这在理论上应该有效。正确。第一个参数被解析为文字字符串。@KevinJensenPetersen尝试将字段名括在方括号中(
[FieldName]
),这可能会起作用。第一个参数是字符串yes,我将其传递到查询中,以便在WHERE中进行测试。我应该如何解决它?我不知道这是否适合您的需要,但您可以尝试使用字符串。在“where”子句中格式化您的firld名称,并将参数设置为实际值。这将使您无法对每个案例进行新的查询。我尝试的是采用灵活的方式进行SQL查询,而不是每次都要进行新的查询,因为我想从数据库中选择一些内容。这就是为什么我使用字段名作为参数,这在理论上应该是可行的。第一个参数被解析为文字字符串。@KevinJensenPetersen尝试将字段名括在方括号中(
[FieldName]
),这可能会起作用。第一个参数是字符串yes,我将其传递到查询中,以便在WHERE中进行测试。我应该如何解决它?我不知道这是否适合您的需要,但您可以尝试使用字符串。在“where”子句中格式化您的firld名称,并将参数设置为实际值。这会阻止你对每个案例进行新的查询。我对查询的实际计划是让它看起来像。。其中“category_id”=1我对查询的实际计划是什么样的。。其中'category_id'=1
mycmd.CommandText="SELECT * FROM categories WHERE @db_property = @property_id"; 

// This contains a string "category_id", which is correct.
mycmd.Parameters.Add("@db_property", SqlDbType.VarChar).Value=db_property_field; 

// This contains an Int, referring to the category_id in database. As of now, this is 1
mycmd.Parameters.Add("@property_id", SqlDbType.Int).Value=property_id; 
mycmd.CommandText = "SELECT * FROM categories WHERE Cast(@db_property as Int) = @property_id";
mycmd.CommandText = "SELECT * FROM categories WHERE category_id = @property_id";
mycmd.Parameters.Add("@property_id", SqlDbType.Int).Value = property_id;
@db_property = @property_id
SELECT * FROM categories WHERE db_property = @db_property AND property_id = @property_id
DECLARE @Var1 VARCHAR(100)
DECLARE @Var2 INT

SELECT @Var1 = 'Test', @Var2 = 1

SELECT * FROM dbo.categories  WHERE @Var1 = @Var2
Create PROCEDURE [dbo].[PmSPValidate]
@a varchar(10)

AS
BEGIN
(SELECT AcctDsc,AcctAge 
FROM dbo.tblCoa 
WHERE AcctNo >= @a)

END

Code of C# :

 private void btnThirdTrial_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection connection;
            SqlDataAdapter adapter;
            SqlCommand command = new SqlCommand();
            SqlParameter param;
            DataSet ds = new DataSet();

            int i = 0;

            connetionString = "Data Source=FIN03;Initial Catalog=CmsTest;Integrated Security=True";

            connection = new SqlConnection(connetionString);

            connection.Open();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "dbo.PmSPValidate";

            param = new SqlParameter("@a",Account.Text.ToString ());
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            adapter = new SqlDataAdapter(command);
            adapter.Fill(ds);

            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                MessageBox.Show(" Name " + ds.Tables[0].Rows[i][0].ToString() + "  Age " + ds.Tables[0].Rows[i][1].ToString());

            }

            connection.Close();

        }