我正在尝试将我的应用程序连接到Microsoft SQL Server 2008 R2并从中检索数据。我可以通过php将我的应用程序连接到MySql,但是就MSSQL服务器而言,它无法完成相同的任务。我用Google搜索这个和这个链接。但是这些链接并没有确切告诉我应该遵循的程序。在这里,在这链接我发现了一些告诉我要使用ASP的东西。但是我无法应付代码。作为一种新方法,有人可以告诉我有关将android应用程序连接到MSSQL服务器的分步过程吗?非常需要该解决方案。另一方面,一些评论和答案告诉我应该使用Web服务。如何在代码中实现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。适用于SQL Server的1433端口。但是连接字符串返回null。
有人可以告诉我如何完成连接吗?感谢您的任何帮助。
终于找到了解决我的问题的安静指导方针。它告诉我有关实现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连接访问位于PC中的Web服务。[如果知道如何纠正我,请提供正确的网址]。在这里,我得到了模拟器,并且任何人都可以肯定地使用他们的真实设备从真实ip访问实时Web服务,我已经对其进行了测试。这段代码非常适合我。这就是我解决问题的方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句