0

我正在尝试将我的应用程序连接到 Microsoft SQL Server 2008 R2 并从中检索数据。我可以通过 php 将我的应用程序连接到 MySql,但在 MSSQL 服务器方面无法完成相同的任务。我已经用谷歌搜索了这个这个 链接。但是这些链接并没有准确地告诉我应该遵循的程序。在 链接 我发现了一些告诉我使用 asp 的东西。但我无法应付代码。作为一个这样做的新人,谁能告诉我有关将android应用程序连接到MSSQL服务器的分步过程?非常需要解决方案。另一方面,一些评论和回答告诉我应该使用网络服务。我应该如何在我的代码中实现 Web 服务。我可以通过 PHP 代码通过 json 解析从 MySql 源中检索数据。我应该使用 ASP 而不是 PHP。那我该怎么做呢?我作为新手所做的如下。

试过直接连接服务器。

public void query()
{
        Log.e("Android"," SQL Connect Example.");
        Connection conn = null;
        try {
            String driver = "net.sourceforge.jtds.jdbc.Driver";
            Class.forName(driver).newInstance();
            //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
            String connString = "jdbc:jtds:sqlserver://192.168.115.16:1433/WSMS_H;user=sa;password=123456;integratedSecurity=true;";
            String username = "";
            String password = "";
            conn = DriverManager.getConnection(connString,username,password);
            Log.e("Connection","open");
            Statement stmt = conn.createStatement();
            ResultSet reset = stmt.executeQuery("select * from dbo.Users");
             
            //Print the data to the console
            while(reset.next()){
            Log.e("Data:",reset.getString(3));
        //                Log.w("Data",reset.getString(2));
            }
            conn.close();
             
        }
        catch (Exception e)
        {
            Log.w("Error connection","" + e.getMessage());
        }
    }

这不是我承认的正确方式。我所拥有的是安装在我的本地机器上的 MS SQL server 2008 R2。1433 端口为 SQL 服务器工作。但连接字符串返回 null。

有人可以告诉我我该怎么做才能完成连接吗?感谢您提供任何帮助。

4

1 回答 1

1

终于找到了一个安静的好指南来解决我的问题。它告诉我有关实现 Web 服务以及如何将 MSSQL 服务器与 Android 连接的详细信息。

这不仅有帮助,而且可以在正确答案中找到详细信息

我需要做的是,首先,必须使用 .NET(C#) 创建一个 Web 服务,然后将我的 android 应用程序连接到该 Web 服务以从本地机器检索数据 [对我来说它是我的 MSSQL Server 2008 R2 ]。希望这对其他人有帮助。但是在实施这些链接中提供的想法和代码之后;我想出了一些改变,特别是在方法上。

这就是我在 MainActivity 中所做的。

希望,这对其他人有帮助。

 private static final String SOAP_ACTION = "http://tempuri.org/findContact";

    private static final String OPERATION_NAME = "findContact";// your webservice web method name

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

    private static final String SOAP_ADDRESS = "http://10.0.2.2:58497/WebService/Service.asmx";

    protected static final String TAG = null;
    private static String fahren;

    TextView tvData1;
    EditText edata;
    Button button;
    String studentNo;
    String state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvData1 = (TextView)findViewById(R.id.textView1);
        edata =(EditText)findViewById(R.id.editText1);

        button=(Button)findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                studentNo=edata.getText().toString();
                new Submit().execute(studentNo);
            }
        });
    }

    private class Submit extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected String doInBackground(String... arg) {
            // TODO Auto-generated method stub
            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
            PropertyInfo propertyInfo = new PropertyInfo();
            propertyInfo.type = PropertyInfo.STRING_CLASS;
            propertyInfo.name = "eid";
            propertyInfo.setValue(studentNo);
            request.addProperty(propertyInfo);//need to be careful adding this, as it became an issue for me while I was getting continuous exception.

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
            httpTransport.debug = true;
            try{ 
                httpTransport.call(SOAP_ACTION, envelope);

                Log.e("RequestDump", httpTransport.requestDump.toString());
                Log.e("ResponseDump", httpTransport.responseDump.toString());

                SoapObject result=(SoapObject)envelope.bodyIn;
                if(result!= null){
                    state = result.getProperty(0).toString();
                    Log.e("Found", state);
                }
                else{
                    Log.e("Obj", result.toString());
                }
            }  
            catch (Exception exception)   {
                Log.e("Exception", exception.toString());
            }
            return state;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            tvData1.setText(result);
        }
    }
}

当然,您无法通过真实设备上的 wifi 连接访问位于您电脑中的网络服务。[如果你知道怎么做,请纠正我,请提供正确的网址]。在这里,我最终使用了模拟器,任何人都可以使用他们的真实设备从真实 IP 访问实时 Web 服务,我已经对其进行了测试。这段代码非常适合我。这就是我解决问题的方法。

于 2014-11-19T05:47:43.580 回答