标签:.net开源 web service c#
本节主要讲解服务器端ASP.NET Web Service数据库配置和操作,客户端数据库操作。HYAppFrame项目地址:https://sourceforge.net/u/chinahysoft/profile/
1. 服务器端数据库操作
HYAppFrame服务器端通过ASP.NET Web Service连接数据库。
数据库连接配置
在文件Web.config配置数据库,需设置数据库服务器地址Data Source、数据库名称Initial Catalog、访问帐号User Id、访问密码Password等。dbName不需修改。
<connectionStrings> <add name="dbName" connectionString="Data Source=localhost;Initial Catalog=hyappframe;Integrated Security=False;User Id=sa;Password=123456;"/> </connectionStrings>
public static SqlConnection OpenDatabase(string connString) { SqlConnection conn = new SqlConnection(); try { conn.ConnectionString = connString; conn.Open(); } catch (Exception ex) { Log("Open database error:" + ex.Message + "\r\n" + ex.StackTrace); } return conn; }
public int ExecNoneQueryBySql(string sql, DataTable paras) { if (!IsLogin()) return -100; SqlConnection conn = MyFuncLib.OpenDatabase(MyFuncLib.connString); int result = 0; try { sql = DES.Decrypt(sql, MyFuncLib.passwordKey); SqlCommand comd = new SqlCommand(); comd.CommandTimeout = 1800; comd.Connection = conn; comd.CommandText = sql; comd.Parameters.Clear(); if (paras != null) { foreach (DataRow row in paras.Rows) { comd.Parameters.AddWithValue(MyFuncLib.Dtv(row, "name", string.Empty), MyFuncLib.Dtv(row, "value", string.Empty)); } } result = comd.ExecuteNonQuery(); } catch (Exception ex) { MyFuncLib.Log(ex.Message + "\r\n" + ex.StackTrace); result = -1; } finally { MyFuncLib.CloseDatabase(conn); } return result; }
[WebMethod(EnableSession = true)] public DataTable ExecQueryByNamed(string queryName, DataTable paras)
DataTable sqlParams = new DataTable("sqlParametersDataTable"); sqlParams.Columns.Add(new DataColumn("name", System.Type.GetType("System.String"))); sqlParams.Columns.Add(new DataColumn("value", System.Type.GetType("System.String"))); DataRow dRow; foreach (ListItem item in paras) { dRow = sqlParams.NewRow(); dRow["name"] = item.Name; dRow["value"] = item.Value; sqlParams.Rows.Add(dRow); } dt = MyFuncLib.WS.ExecQueryBySql(DES.Encrypt(sql, MyFuncLib.PasswordKey), sqlParams);
string sql = "select 1"; string str = MyFuncLib.DBCommandExecScalarBySql(sql, null);
string sql = "select 1 from yourtable where ID_ = @id"; ArrayList sqlParams = new ArrayList(); sqlParams.Add(new ListItem("@id", 1)); string str = MyFuncLib.DBCommandExecScalarBySql(sql, sqlParams);
HYAppFrame数据库开发入门(ASP.NET Web Service)
标签:.net开源 web service c#
原文地址:http://blog.csdn.net/daiqianjie/article/details/44180119