且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在UWP中连接到MS SQL服务器

更新时间:2023-02-03 23:34:28

UWP中不存在数据读取器,数据集,sqlconnection等Ado.Net之类的东西。  您可以将实体框架用于SQLite数据库。  如果要连接到Sql Server,则需要通过Web服务进行。




On a standard cs winforms/asp.net application I can use System.Data.SqlConnection to connect to my server something like:

List<Video> rv = new List<Video>(); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(String.Format("SELECT * FROM Video"), connection); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { IDataReader record = reader; string name = String.Format("{0}", record[0]).Trim(); string id = String.Format("{0}", record[1]).Trim(); string src = String.Format("{0}", record[2]).Trim(); string metaData = String.Format("{0}", record[3]).Trim(); string discription = String.Format("{0}", record[4]).Trim(); string datePosted = String.Format("{0}", record[5]).Trim(); string status = String.Format("{0}", record[6]).Trim(); string privateHiden = String.Format("{0}", record[7]).Trim(); string views = String.Format("{0}", record[8]).Trim(); Video video = new Video(name, id, datePosted, metaData, discription, src, status, privateHiden, views); rv.Add(video); } reader.Close();

To return a list.

Is there any way to use this in universal windows?

None of the Ado.Net stuff like data readers, data set, sqlconnection, etc exist in UWP.  You can use entity framework to a SQLite data base.  If you want to connect to a Sql Server you need to do it through a web service.