简体   繁体   English

如何从Sql Server数据库中检索数据?

[英]How to retrieve data from Sql Server database?

I am new to Android. 我是Android新手。 I am already connected to the SQL Server and I want to retrieve data from the database and show it in a Table. 我已经连接到SQL Server,并且想从数据库中检索数据并将其显示在表中。

Here is the code: 这是代码:

public  void QuerySQL(String COMANDOSQL) {

    try {
        //String sql="select * from Activity";
        Statement stmt = ma.connect.createStatement();


        stmt.executeQuery("SELECT * FROM "+excbtn.getText().toString()+"");
        ResultSet rs=stmt.getResultSet();
        ResultSetMetaData rsmd= rs.getMetaData();
        int colcount=rsmd.getColumnCount();
        int j=0;

        while(rs.next()){
            j++;
            for(int i=1; i<=colcount; i++)
            {
                String ss=rsmd.getColumnName(i);
                // create a new TextView

                if(j<=colcount){
                t = new TextView(this);
                t.setText(ss);
                t.setWidth(200);//Set to any meaningful text
                t.setBackgroundColor(Color.YELLOW);
                tr.addView(t); //Attach TextView to its parent (row)
                TableRow.LayoutParams params =
                        (TableRow.LayoutParams)t.getLayoutParams();
                params.column= i; //place at ith columns.

                params.span = 1; //span these many columns,

                params.setMargins(2,2,2,2);

                params.width = TableRow.LayoutParams.FILL_PARENT;

                params.height = TableRow.LayoutParams.WRAP_CONTENT;
                t.setPadding(2, 2, 2, 2);

                t.setLayoutParams(params);

                    //colhead=true;
               }


                    t1 = new TextView(this);
                    String tabval=rs.getString(ss);
                    t1.setText(rs.getString(ss));
                    t1.setWidth(200);//Set to any meaningful text
                   // t1.setBackgroundColor(Color.YELLOW);
                    tr1.addView(t1); //Attach TextView to its parent (row)
                    TableRow.LayoutParams params1 =
                            (TableRow.LayoutParams)t.getLayoutParams();
                    params1.column= i; //place at ith columns.

                    params1.span = j; //span these many columns,

                    params1.setMargins(2,2,2,2);

                    params1.width = TableRow.LayoutParams.FILL_PARENT;

                    params1.height = TableRow.LayoutParams.WRAP_CONTENT;
                    t1.setPadding(2, 2, 2, 2);
                    t1.setLayoutParams(params1);

                    //String from = {ss1};


                }

How to bind the data and where to execute table row? 如何绑定数据以及在哪里执行表行?

By assuming your database on remote server- 通过假设您的数据库位于远程服务器上,

1)You need a WebService to retrieve data from the server. 1)您需要一个WebService来从服务器检索数据。

2)After retrieving the data from server you need parse it using its format eg JSON,XML 2)从服务器检索数据后,您需要使用其格式(例如JSON,XML)对其进行解析

3)After parsing the data you can manipulate or store it as per your application need. 3)解析数据后,您可以根据您的应用程序需求对其进行操作或存储。

(right now,focus on how to retrieve data from server using WebService, once you have data,focus on your UI part ) (现在,专注于如何使用WebService从服务器检索数据,一旦获得数据,请专注于UI部分)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM