public static int Add(DeviceInfo device) { SqlParameter[] parameters = new SqlParameter[9]; parameters[0] = new SqlParameter("@IP", SqlDbType.VarChar, 30); parameters[0].Value = device.IP; parameters[1] = new SqlParameter("@Port", SqlDbType.VarChar, 10); parameters[1].Value = device.Port; parameters[2] = new SqlParameter("@DeviceType", SqlDbType.VarChar, 2); parameters[2].Value = device.DeviceType; parameters[3] = new SqlParameter("@UserName", SqlDbType.VarChar, 50); parameters[3].Value = device.UserName; parameters[4] = new SqlParameter("@Password", SqlDbType.VarChar, 50); parameters[4].Value = device.Password; parameters[5] = new SqlParameter("@AntNo", SqlDbType.Int); parameters[5].Value = device.AntNo; parameters[6] = new SqlParameter("@AccessFlag", SqlDbType.Int); parameters[6].Value = device.AccessFlag; parameters[7] = new SqlParameter("@Flag", SqlDbType.Int); parameters[7].Value = device.Active ? 1 : 0; parameters[8] = new SqlParameter("@Location", SqlDbType.NVarChar, 50); parameters[8].Value = device.Location; StringBuilder sql = new StringBuilder(); sql.AppendLine("DECLARE @ID int"); sql.AppendLine("INSERT INTO DevInfo (devIp, devPort, devType, devUserName, devPassword, antNo, accessFlag, flag, Location)"); sql.AppendLine("VALUES (@IP, @Port, @DeviceType, @UserName, @Password, @AntNo, @AccessFlag, @Flag, @Location)"); sql.AppendLine("SET @ID = SCOPE_IDENTITY()"); sql.AppendLine("SELECT @ID"); int newID = (int)helper.ExecuteScalar(sql.ToString(), parameters); return newID; }
public static int Save(DeviceInfo device) { int id; if (device.ID <= 0) { id = DeviceDao.Add(device); } else { id = DeviceDao.Update(device) ? device.ID : 0; } return id; }
public static bool Update(DeviceInfo device) { SqlParameter[] parameters = new SqlParameter[10]; parameters[0] = new SqlParameter("@IP", SqlDbType.VarChar, 30); parameters[0].Value = device.IP; parameters[1] = new SqlParameter("@Port", SqlDbType.VarChar, 10); parameters[1].Value = device.Port; parameters[2] = new SqlParameter("@DeviceType", SqlDbType.VarChar, 2); parameters[2].Value = device.DeviceType; parameters[3] = new SqlParameter("@UserName", SqlDbType.VarChar, 50); parameters[3].Value = device.UserName; parameters[4] = new SqlParameter("@Password", SqlDbType.VarChar, 50); parameters[4].Value = device.Password; parameters[5] = new SqlParameter("@AntNo", SqlDbType.Int); parameters[5].Value = device.AntNo; parameters[6] = new SqlParameter("@AccessFlag", SqlDbType.Int); parameters[6].Value = device.AccessFlag; parameters[7] = new SqlParameter("@Flag", SqlDbType.Int); parameters[7].Value = device.Active ? 1 : 0; parameters[8] = new SqlParameter("@ID", SqlDbType.Int); parameters[8].Value = device.ID; parameters[9] = new SqlParameter("@Location", SqlDbType.NVarChar, 50); parameters[9].Value = device.Location; StringBuilder sql = new StringBuilder(); sql.AppendLine("UPDATE DevInfo SET devIp = @IP, devPort = @Port"); sql.AppendLine(" , devType = @DeviceType, devUserName = @UserName, devPassword = @Password"); sql.AppendLine(" , antNo = @AntNo, accessFlag = @AccessFlag, flag = @Flag, Location = @Location"); sql.AppendLine("WHERE ID = @ID"); return helper.ExecuteNonQuery(sql.ToString(), parameters.ToArray()) > 0; }