参数化查询'(@ id nvarchar(4000),@ name nvarchar(4000),@ project nvarchar(4000)'需要参数'@id',这是未提供的

时间:2013-08-27 22:43:34

标签: c# sql-server

通过wcf将数据存储到数据库中时出现错误。我通过文本框从android客户端获取输入。 错误:参数化查询'(@ ID nvarchar(4000),@ Name nvarchar(4000),@ Project nvarchar(4000)'需要参数'@ID',这是未提供的。

数据插入的c#代码是

 [DataContract]
    public class studentinfo
    {
        [DataMember(Name = "ID")]
        public string ID { get; set; }

        [DataMember(Name = "Name")]
        public string Name { get; set; }

        [DataMember(Name = "Project")]
        public string Project { get; set; }

        [DataMember(Name = "Result")]
        public string Result { get; set; }


    }

  [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "SaveData")]
        void SaveData(studentinfo studentinfo);



 public void SaveData(studentinfo studentinfo)
    {
        SqlConnection con;


        con = new SqlConnection(ConfigurationManager.ConnectionStrings["Myproject.Properties.Settings.MyConnection"].ConnectionString);

        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }



    SqlCommand cmd = new SqlCommand("INSERT INTO Result (ID,Name,Project,Result) values(@ID,@Name,@Project,@Result)", con);

    cmd.Parameters.AddWithValue("@ID", studentinfo.ID);

    cmd.Parameters.AddWithValue("@Name", studentinfo.Name);

    cmd.Parameters.AddWithValue("@Project", studentinfo.Project);

    cmd.Parameters.AddWithValue("@Result", studentinfo.Result);


    int Valid = cmd.ExecuteNonQuery();



    con.Close();

}

编辑:这里是android客户端代码:

public class MarksActivity extends Activity {

     private final static String SERVICE_URI = "http://10.0.2.2:51220/Service1.svc";
    Button btnsave, btncal;
    EditText txtid, txtname, txtproject, txtviva, txtpres, txtconfi, txtsystem, txttotal;
    double h=0;
    double s=0;
    double x=0;
    double y=0;
    double z=0;
    String text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marks);

        btnsave=(Button)findViewById(R.id.btnsave);

        text    = "";

        btncal=(Button)findViewById(R.id.btncal);


        txtid=(EditText)findViewById(R.id.txtid);
        txtname=(EditText)findViewById(R.id.txtname);
        txtproject=(EditText)findViewById(R.id.txtproject);
        txttotal=(EditText)findViewById(R.id.txttotal);

        txtviva=(EditText)findViewById(R.id.txtviva);
        txtpres=(EditText)findViewById(R.id.txtpres);
        txtconfi=(EditText)findViewById(R.id.txtconfi);
        txtsystem=(EditText)findViewById(R.id.txtsystem);

        btnsave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                saveData();
                }
            });


}



    public void saveData() {




        try{

            boolean isValid = true;


                h=Double.parseDouble(txtviva.getText().toString());
                s=Double.parseDouble(txtpres.getText().toString());
                x=Double.parseDouble(txtconfi.getText().toString());
                y=Double.parseDouble(txtsystem.getText().toString());
                  z= h+s+x+y;
                  txttotal.setText(Double.toString(z));




                  if (isValid) {

                        // POST request to <service>/SaveVehicle


                    //Connect to the server
                    HttpPost httpPost=new HttpPost(SERVICE_URI +"/SaveData");
                    httpPost.setHeader("Accept", "application/json");
                    httpPost.setHeader("Content-type", "application/json");

                        // Build JSON string
                        JSONStringer getdata = new JSONStringer()
                            .object()
                                .key("studentinfo")
                                    .object()
                                        .key("ID").value(txtid.getText().toString())
                                        .key("Name").value(txtname.getText().toString())
                                        .key("Project").value(txtproject.getText().toString())
                                        .key("Result").value(txttotal.getText().toString())
                                    .endObject()
                                .endObject();
                        StringEntity entity = new StringEntity(getdata.toString());

                        httpPost.setEntity(entity);

                        // Send request to WCF service
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpResponse response = httpClient.execute(httpPost);

                        Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());

                        // Reload plate numbers

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }


        }

    }

我确实尝试过其他答案中提供的一些方法但无法解决此问题。 请帮我解决一下

1 个答案:

答案 0 :(得分:0)

您的studentinfo.ID值可能是null?如果要将null作为参数传递,则需要使用DBNullhttp://msdn.microsoft.com/en-us/library/system.dbnull.aspx

杰森