是否可以在Xamarin中将SQL Server与Andriod应用程序连接? [英] Is it possible to connect SQL Server with Andriod app in Xamarin?

查看:155
本文介绍了是否可以在Xamarin中将SQL Server与Andriod应用程序连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Asp.net网络项目,我需要创建一个与C#编码相同功能的Andriod应用程序,我们可以在Xamarin中连接SQL Server吗?



是否有可能开发一个具有gridview功能的Android应用程序,选择索引更改,rowdatabound等等。

解决方案

是。

阅读本文 - http://developer.xamarin.com/guides/cross-platform/Application_Fundamentals/ Web_Services / [ ^ ]


如果将SQL调用包装在Web服务中,则可以在Android应用程序中调用它们。






是的,您可以创建在web asp.net网格等中开发的所有功能。



对于网格函数,使用列表视图,可以自定义设计以查看网格中的所有项目。



用于将其连接到SQL Server编写WCF / JSON格式服务..并在Web服务器中托管服务。





从Xamarin Android上传数据到远程服务器





列表listToServer = 列表<学生> (); 
尝试
{
listToServer.Clear();


new Java.Lang.Thread(()= >
{
listToServer.Add(exCloud);
string jsonstr = JsonConvert.SerializeObject(Student);

ASCIIEncoding encoding = new ASCIIEncoding();

byte [ ] data = encoding.GetBytes(jsonstr);


string webStr = http://sampleStudent.com/wcf/ExcelData.svc/json/StudentDetails;

HttpWebRequest webrequest =(HttpWebRequest )WebRequest.Create(webStr);
webrequest.Method = POST;
webrequest.ContentType = application / x-www-form-urlencoded;
webrequest.ContentLength = data.Length;
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0 ,data.Length);
newStream.Close();
HttpWebResponse webresponse =(HttpWebResponse)webrequest.GetResponse();
编码enc = System.Text.Encoding.GetEncoding( utf-8);
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(),enc);
string message = loResponseStream.ReadToEnd();


RunOnUiThread(()= > onSuccessfulLogincl(message));

})。Start();






}
catch (例外情况ex)
{

Toast.MakeText( this ,ex.Message,ToastLength.Long).Show();
}







从Android Xamarin向服务器发送数据



 ListgetDataFromServer()
{
List StudentList = new List();
try
{
WebClient client = new WebClient();
string webStr;




webStr = http:/ /sampleStudent.com/wcf/ExcelData.svc/json/students;
HttpWebRequest webrequest =(HttpWebRequest)WebRequest.Create(webStr);

webrequest.Method = GET;
webrequest.ContentType = application / x-www-form-urlencoded;

HttpWebResponse webresponse =(HttpWebResponse)webrequest.GetResponse();
编码enc = System.Text.Encoding.GetEncoding( utf-8);
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(),enc);

string JsonString = loResponseStream.ReadToEnd();




StudentList = Deserialise> (JsonString);


webresponse.Close();
loResponseStream.Close();


RunOnUiThread(()=> HideProgress());

}
catch (例外情况)
{
Toast.MakeText( this ,ex.Message,ToastLength.Long).Show();
}


return StudentList;



}



public static T Deserialise( string json){
var obj = Activator.CreateInstance();
使用 var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json))){
var serializer = new DataContractJsonSerializer(obj.GetType());
obj =(T)serializer.ReadObject(memoryStream);
return obj;
}
}









< br $>








谢谢,

Ullas克里希南


I have developed an Asp.net web project and i need to create an Andriod app same functionality as in C# coding and can we connect with SQL Server in Xamarin ?

is it possible to develop an android app with the functionality like gridview with selected index change,rowdatabound and many more.

解决方案

Yes.
Read this - http://developer.xamarin.com/guides/cross-platform/Application_Fundamentals/Web_Services/[^]


If you wrap your SQL calls in a web service then you can invoke them in your Android app.


Hi,

Yes you can create all functionality which developed in web asp.net grid etc..

For Grid functions use List View, which can be custom designed to view all items in a grid ..

For connecting it to SQL Server write WCF / JSON format service.. and host the service in web server.


Upload Data from Xamarin Android to Remote Server


List    listToServer = new List< Student >();
            try
            {
                listToServer.Clear();
                
                     
                new Java.Lang.Thread(() =>
                    {
                       listToServer.Add(exCloud);
                         string jsonstr =   JsonConvert.SerializeObject (Student);

                        ASCIIEncoding encoding = new ASCIIEncoding();

                        byte[] data = encoding.GetBytes(jsonstr);

 
                        string webStr = "http://sampleStudent.com/wcf/ExcelData.svc/json/StudentDetails";

                        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(webStr);
                        webrequest.Method = "POST";
                        webrequest.ContentType = "application/x-www-form-urlencoded";
                        webrequest.ContentLength = data.Length;
                        Stream newStream = webrequest.GetRequestStream();
                        newStream.Write(data, 0, data.Length);
                        newStream.Close();
                        HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
                        Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                        StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
                        string message=loResponseStream.ReadToEnd();


                        RunOnUiThread(() => onSuccessfulLogincl(message));

                    }).Start();




              
                
            }
            catch (Exception ex)
            {

                Toast.MakeText (this, ex.Message, ToastLength.Long).Show ();
            }




Sending Data to Server from Android Xamarin

ListgetDataFromServer()
        {
            List StudentList = new List ();
            try
            {
                WebClient client = new WebClient ();
                string webStr;




                webStr = "http://sampleStudent.com/wcf/ExcelData.svc/json/students";
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create (webStr);

                webrequest.Method = "GET";
                webrequest.ContentType = "application/x-www-form-urlencoded";

                HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse ();
                Encoding enc = System.Text.Encoding.GetEncoding ("utf-8");
                StreamReader loResponseStream = new StreamReader (webresponse.GetResponseStream (), enc);

                string JsonString = loResponseStream.ReadToEnd ();




                StudentList = Deserialise> (JsonString);


                webresponse.Close ();
                loResponseStream.Close ();


                RunOnUiThread (() =>HideProgress () );

            }
            catch (Exception ex)
            {
                Toast.MakeText (this,ex.Message, ToastLength.Long).Show ();
            }


            return StudentList;



        } 



 public static T Deserialise(string json) {
            var obj = Activator.CreateInstance();
            using (var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json))) {
                var serializer = new DataContractJsonSerializer(obj.GetType());
                obj = (T) serializer.ReadObject(memoryStream);
                return obj;
            }
        }










Thanks,
Ullas Krishnan


这篇关于是否可以在Xamarin中将SQL Server与Andriod应用程序连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆