簡體   English   中英

參數化查詢'((@id nvarchar(4000),@ name nvarchar(4000),@ project nvarchar(4000)')期望參數'@id',未提供

[英]The parameterized query '(@id nvarchar(4000),@name nvarchar(4000),@project nvarchar(4000)' expects the parameter '@id', which was not supplied

通過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();
                }


        }

    }

我確實嘗試了其他答案中提供的某些方法,但無法解決此問題。 請幫我解決

您的studentinfo.ID值可能為null嗎? 如果要傳遞null作為參數,則需要使用DBNullhttp://msdn.microsoft.com/en-us/library/system.dbnull.aspx

傑森

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM