如何在sql查询中传递整数变量

use*_*309 2 sql c#-4.0

int no = FormView1.PageIndex;
Run Code Online (Sandbox Code Playgroud)

查询: -

  SqlCommand cmd =  new SqlCommand("select Answer from Questions where QuestionNo = @no", cn);
Run Code Online (Sandbox Code Playgroud)

Ree*_*sey 5

你必须添加一个参数:

int no = FormView1.PageIndex;
SqlCommand cmd = 
    new SqlCommand("select Answer from Questions where QuestionNo = @no", cn);

// Set the parameter up before executing the command
cmd.Parameters.Add("@no", SqlDbType.Int);
cmd.Parameters["@no"].Value = no;
Run Code Online (Sandbox Code Playgroud)