如何从以下语法中访问c#词典元素?

时间:2017-01-30 07:55:07

标签: c# mysql .net dictionary generics

我创建了一个表示xls表单元格值的列表,它看起来像这样,后跟一个INSERT语句,我将用它将值放在某个数据库中:

class ConnectSqlDb extends AsyncTask<String, Void, String> {
        ProgressDialog waitDialog;
        @Override
        protected void onPreExecute() {
            waitDialog=ProgressDialog.show(MainActivity.this,"Connecting to MySQLServer","Please wait...",true,false);
        }

        @Override
        protected String doInBackground(String... params) {
            URL url=null;
            HttpURLConnection connection=null;
            OutputStream os;
            BufferedWriter writer;
            StringBuilder parameter=new StringBuilder();
            try {
                parameter.append(URLEncoder.encode("host", "UTF-8"));
                parameter.append("=");
                parameter.append(URLEncoder.encode(params[1], "UTF-8"));
                parameter.append("&");

                parameter.append(URLEncoder.encode("user", "UTF-8"));
                parameter.append("=");
                parameter.append(URLEncoder.encode(params[2], "UTF-8"));
                parameter.append("&");

                parameter.append(URLEncoder.encode("pass", "UTF-8"));
                parameter.append("=");
                parameter.append(URLEncoder.encode(params[3], "UTF-8"));
            }catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            try {
                url = new URL(params[0]);
                connection = (HttpURLConnection)url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                os=connection.getOutputStream();
                writer=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
                writer.write(parameter.toString());
                writer.flush();
                writer.close();

                BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String message=br.readLine();
                br.close();
                return message;
            }catch (MalformedURLException e){
                e.printStackTrace();
                return null;
            }
            catch ( IOException e){
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(String message) {
            waitDialog.dismiss();
            Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();

        }
    }
public void connect(View v){
        String server=phpserver.getText().toString();
        String hostname=host.getText().toString();
        String username=user.getText().toString();
        String password=pass.getText().toString();
        new ConnectSqlDb().execute(server,hostname,username,password);

    }

当我创建 createQuery2 时,它返回(在VALUES内)以下内容:

  

System.Collections.Generic.Dictionary List<FieldValues> fieldValues = new List<FieldValues>() { new FieldValues(tableFields[0]) { dictionary = new Dictionary<int, string>() { {0, "" }, {1, "" } } }, new FieldValues(tableFields[1]) { dictionary = new Dictionary<int, string>() { {0, "x" }, {1, "x" } } }, new FieldValues(tableFields[2]) { dictionary = new Dictionary<int, string>() { {0, "x" }, {1, "x" } } }, new FieldValues(tableFields[3]) { dictionary = new Dictionary<int, string>() { {0, "Car" }, {1, "Travel" } } }, }; string createQuery2 = createQuery + "\n INSERT INTO `poi_specs_mgu_ece_1.6` ("; for (var i = 0; i < tableFields.Count; i++) { createQuery2 += "\n\t" + tableFields[i].GetFieldDeclaration() + (i == tableFields.Count - 1 ? string.Empty : ","); } createQuery2 += ")\n VALUES ( "; for (var i = 0; i < fieldValues.Count; i++) { createQuery2 += "\n\t" + fieldValues[i].dictionary; //+ (i == fieldValues.Count - 1 ? string.Empty : ","); } createQuery2 += " \n);"; 2 [System.Int32,System.String]     System.Collections.Generic.Dictionary 2[System.Int32,System.String] System.Collections.Generic.Dictionary 2 [System.Int32,System.String]

而不是字典中的值。 有人可以帮助我更好地想象问题吗?为什么我不能阅读字典元素?

1 个答案:

答案 0 :(得分:1)

在这部分代码中:

createQuery2 += "\n\t" + fieldValues[i].dictionary;

当字典作为对象时,您将字典添加到字符串中,因此它会尝试将您的对象转换为字符串,而该字符串只返回该类型。

您需要直接访问字典值。

像这样:

createQuery2 += "\n\t" + fieldValues[i].dictionary[0] + fieldValues[i].dictionary[1];