废话
最近一个客户需要开发一个程序,c#开发速度没有php来得快,又急的上线,php原先做了一套类似的系统,所以直接用php了,但是客户使用的是sql server 数据库,所以要从原来的mysql 数据库那边转移过来。情况又比较曲折,原系统开发商只提供sql的存储过程,所以只能用think去处理了,发现没执行出来,后来又曲折的做了好多测试,微软关于php插件方便的接口文档也看了一遍,没找到相应的解决方案,所以只做了一半,先做一下记录待以后慢慢解决,后来自己写了一份C#的webservice去处理了。
开发环境
windows10
phpstudy2016(php5.4,SQL SERVER2008R2)
Microsoft SQL Server 2012 Native Client.msi
测试代码
$dbHost=""; // 服务器地址 $dbName = ''; // 数据库名 $dbUser = ''; // 用户名 $dbPass = ''; // 密码 $serverName = $dbHost; // 这里是数据库所在计算机的ip地址 //第一个参数是选择的数据库, 第二个是用户名,第三个是密码 $connectionInfo = array("Database" => $dbName, "UID" => $dbUser, "PWD" => $dbPass); // $connectionInfo = array("UID"=>"username", "PWD"=>"password", "Database"=>"db", "CharacterSet"=>"utf-8"); $conn = sqlsrv_connect($serverName, $connectionInfo); if($conn === false){ die(print_r(sqlsrv_errors(), true)); } $tid = 'ID'; $str=2000000000000028; $params = array( array($tid, SQLSRV_PARAM_IN), array($str, SQLSRV_PARAM_IN) //array($kind, SQLSRV_PARAM_OUT) ); $sp = "{call RT_BARTAA_PRINT(?,?)}"; $stmt = sqlsrv_query($conn, $sp, $params); if($stmt === false){ echo "111"; die(print_r(sqlsrv_errors(), true)); }else{ echo "success done"; } while($row = sqlsrv_fetch_array($stmt,SQLSRV_SCROLL_FIRST)){ echo "111"; echo $row["account"]."<br/>"; }
结论
此代码能够连接,也能执行,但是无法返回结果,暂时没想明白为什么不行,可能调用(或者代码使用有问题)。
最后处理(C#代码)
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Web; using System.Web.Services; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class Service : System.Web.Services.WebService { public Service () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string getsqldone(string sql) { /* * 传递参数: * 1.查询语句(查询语句的格式:查询语句1|查询语句2|查询语句3) * 2.需要查询的字段(查询的字段1|查询字段2@查询字段11|查询字段22) * **/ SqlConnection conn = new SqlConnection("server=;uid=;pwd=;database="); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); SqlDataReader websqlr = comm.ExecuteReader(); //return "111"; return ToJson(websqlr); } [WebMethod] public string gettest(string testkey) { return testkey; } public static string ToJson(SqlDataReader dataReader) { StringBuilder jsonString = new StringBuilder(); //jsonString.Append("["); while (dataReader.Read()) { jsonString.Append("{"); for (int i = 0; i < dataReader.FieldCount; i++) { Type type = dataReader.GetFieldType(i); string strKey = dataReader.GetName(i); string strValue = dataReader[i].ToString(); jsonString.Append("\"" + strKey + "\":"); strValue = String.Format(strValue, type); //datetime不能出现为空的情况,所以将其转换成字符串来进行处理。 //需要加""的 if (true) { if (i <= dataReader.FieldCount - 1) { jsonString.Append("\"" + strValue + "\","); } else { jsonString.Append(strValue); } } //不需要加""的 else { if (i <= dataReader.FieldCount - 1) { jsonString.Append("" + strValue + ","); } else { jsonString.Append(strValue); } } } jsonString.Append("},"); } dataReader.Close(); jsonString.Remove(jsonString.Length - 3, 3); jsonString.Append("}"); //jsonString.Append("]"); return jsonString.ToString(); } }
执行结果