title: SQLServer · 最佳实践 · 开发基于.NET CORE的LINUX版本的数据库应用
最近有客户在基于.NET CORE的LINUX版本连接数据库的应用程序,在开发中,会遇到一些问题,客户会错误地将原因定位到我们的SQL SERVER,陆续收到一些工单,因此,我们需要有计划增强这个方面的能力,同事正确引导用户使用SQL SERVER。
https://www.microsoft.com/net/core#ubuntu
使用的是C#语言,连接的数据库是RDS for SQL SERVER 2008 R2,有GITHUB上文章介绍,连接SQL SERVER 2008 R2需要SP3,但实际上是不需要的。这个客户会误解!
touch DbApp.cs vim DbApp.cs chmod +755 DbApp.cs ---------------------------------------------------------------------------------------------- using System; using System.Data.Common; using System.Data.SqlClient; namespace ConsoleApplication { public class Program { public static void Main(string[] args) { String sqlConnStr="Data Source=***********,3433;User ID=****;Password=****;Initial Catalog=master"; String sqlStr="select spid,waittype from sys.sysprocesses"; SqlConnection sqlConn= new SqlConnection(sqlConnStr); SqlCommand sqlCmd = new SqlCommand(sqlStr,sqlConn); try { string spid=String.Empty; string program_name=String.Empty; sqlConn.Open(); SqlDataReader reader = sqlCmd.ExecuteReader(); while(reader.Read()) { spid=reader[0].ToString(); program_name=reader[1].ToString(); Console.WriteLine("spid="+spid+" "+"waittype="+program_name); } //Console.WriteLine("The server is:"+svrName); } catch(SqlException e) { Console.WriteLine(e.Message); } finally { sqlConn.Close(); } //Console.Read(); } } }默认情况下,system.data.*包是不存在的(.Nuget/package),因此需要添加包的依赖"System.Data.Common": "4.1.0","System.Data.SqlClient" : "4.1.0","System.Runtime": "4.1.0"
touch project.json vim project.json ---------------------------------------------------------------------------------------------- { "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "dependencies": { "System.Data.Common": "4.1.0", "System.Data.SqlClient" : "4.1.0", "System.Runtime": "4.1.0" }, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" } }, "imports": "dnxcore50" } } }找当前目录下的项目文件(project.json),然后利用NuGet库还原整个项目的依赖库,然后遍历每个目录,生成项目文件,继续还原该项目文件中的依赖项。
dotnet restoreOK,第一个简单的数据库程序就完成了。熟悉C#的同学开发起来非常简单!
相关资源:七夕情人节表白HTML源码(两款)